一、简介
1.什么是BDD
Python语言的行为驱动开发,Behavior-driven development,简称BDD。Behavior-driven development (or BDD) is an agile software development technique that
encourages collaboration between developers, QA and non-technical or business participants in a software project.(行为驱动开发(BDD)是一种敏捷软件开发技术。
鼓励软件项目中的开发人员、QA和非技术或业务参与者之间的协作。)
BDD(Behavior Driven Development)是从测试驱动开发(TDD)发展而来的一种软件开发方法。
BDD将测试用例开发以简单的英文语句形式包含在feature file中,该文件是人工生成的。测试用例语句基于系统的行为并且更加以用户为中心。
BDD 是用简单的英语语句而不是典型的编程语言编写的,这改善了技术和非技术团队与利益相关者之间的沟通。
What is Behave:
· behave is behaviour-driven development, Python style.
. behave operates on directories containing:
· feature files written by your Business Analyst / Sponsor / whoever with your behaviour scenarios in it, and a steps directory with Python step implementations for the scenarios.
我们可以通过BDD方法实现测试用例的自动化。Feature file可以包含我们必须自动化的多个场景--.Feature,每个场景都包含多个步骤。
2.BDD的特点
(1)强合作
BDD 在相关方之间提供了强有力的协作,这是因为用英语编写的简单测试用例。在Cucumber测试中,股东在建设性讨论中起着至关重要的作用,因为只有他们知道对软件的期望。由于简单的英语语言,每个人都对项目的进展有很强的可见性。
(2)软件设计遵循商业价值
BDD 非常重视业务价值和需求。通过与客户一起设置优先级,根据他们提供的价值,开发人员能够给出更好的结果,因为他们对客户的想法有深刻的理解。
(3)无处不在的语言
正如前面提到的,测试用例是用无处不在的语言编写的,这对于团队的所有成员来说都是可以理解的,无论他们是否来自技术领域。这有助于减少与概念相关的成员之间的误解。无处不在的语言使新成员可以轻松加入工作。
(4)软件开发满足用户需求
BDD关注业务的需求,让用户满意,当然,用户满意就意味着业务的增长。使用BDD,测试人员专注于比实现具有更大影响的行为。
(5)开发团队更有信心
使用 BDD 的团队通常更有信心,因为他们不会破坏代码,而且当涉及到他们的工作时,会做出更好的预测。
(6)降低成本
通过提高代码质量,BDD基本上降低了维护成本,将项目风险降到最低。
3.Cucmber测试框架
二、环境准备
Install Python
Install Pycharm IDE (Pycharm community is fine)
Install Selenium library (install by cmd or pycharm IDE)
Install Behave
如果没装过Behave: pip install behave
如果已经装了Behave,为了upgrade: pip install-U behave
Install Gherkin plugin.
From Settings - Plugin
Install Chromedriver.
pip install chromedriver-py==125.0.6422.141
三、认识BDD project
1.Create project
2.Install package
在Pycharm terminal安装package
pip install selenium
pip install behave
pip install chromedriver-py==126.0.6478.126
webdriver和chromedriver
WebDriver是一个接口,而ChromeDrixer是一个实现WebDriver接口的类。实际上,ChromeDriver 扩展了实现 WebDriver 的 RemoteWebDriver.
WebDriver是一个通用的自动化测试工具,而ChromeDriver是专门为Chrome浏览器设计的WebDriver实现。要使用 Selenium控制浏览器,需要同时使用WebDriver和ChromeDriver。WebDriver提供了一套标准的接口和协议,而 ChromeDriver则实现了这套协议以特定于Chrome浏览器的形式。WebDriver通过原生浏览器支持或浏览器扩展来直接控制浏览器,WebDriver是针对各个浏览器而开发的,与浏览器紧密集成,支持创建更高级的测试。(Selenium和Webdriber合并,install selenium后,webdriver也包括在内)。
3.feature file
4.step parameters
5.Scenario outline
Scenario和Scenario outline的区别
Scenario:A scenario is a sequence of steps that represents a behavior of the application as expected by the user.
Scenarios are written using the "Scenario" keyword followed by a descriptive title. Steps for the test case are provided using Given, When, and Then steps (or And/But for additional steps). The Scenario is not parameterized, meaning it uses fixed values for input and checks against predefined expected outcomes.
Scenario:场景是一系列步骤,表示用户期望的应用程序行为。
场景是使用“场景”关键字后跟描述性标题编写的。测试用例的步骤使用Given、When和Then步骤(或and/But用于其他步骤)提供。场景不是参数化的,这意味着它使用固定值作为输入,并检查预定义的预期结果。
Scenario outline: Scenario Outline is used when the same test is performed multiple times with a different combination of values. It uses the "Scenario Outline" keyword and includes placeholders (usually enclosed in angled brackets "<>") in the steps where data varies. The actual data sets are provided in a data table using the "Examples" keyword, and each row of the data table represents a separate test instance. Scenario Outlines are particularly useful for data-driven testing, allowing you to run the same scenario with different inputs to ensure the application behaves correctly under various conditions.
Scenario outline:当使用不同的值组合多次执行相同的测试时,使用Scenario outline。它使用“Scenario Outline”关键字,并在数据变化的步骤中包含占位符(通常用尖括号“<>”括起来)。使用“Examples”关键字在数据表中提供实际数据集,数据表的每一行表示一个单独的测试实例。场景大纲对于数据驱动测试特别有用,允许您使用不同的输入运行相同的场景,以确保应用程序在各种条件下都能正确运行。
同样的Scenario会被重复多次。
例子:
6.background
background是公共步骤。