1、什么是BDD测试?
BDD(Behavior Driven Development)测试,即 行为驱动开发测试,是一种基于用户行为和需求的软件测试方法。通过将测试用例编写为自然语言脚本,BDD测试可以促进业务需求、开发和测试团队之间的沟通和协作,从而提高代码的可读性、可维护性和可重复性。
BDD测试的优点在于,它能够将开发、测试和业务部门融合起来,提高效率和质量。通过BDD测试,可以促进团队成员之间的沟通和协作,从而更好地满足业务需求,减少错误,加快发布速度。
在BDD中,常用的概念如下
- Epic: 史诗,一般指一个版本或一批功能更新
- Feature: 特性,一般指一个功能点,如登录,添加商品,查询商品等,在测试中对应一个测试套件(test suite)
- Scenario:场景,即Story,一个明确的场景,对应一个测试用例(test case)
- Step: 步骤,测试步骤有Given/When/Then三种
– Given: 假设,给定数据或前置条件,对应测试中的setup
– When: 当…时,对应一个测试步骤
– Then: 然后,即期望结果,对应一个测试断言
– And: 同上,可以用于Given/When/Then后
行为驱动多用于UI层的测试。因此BDD框架的自动化一般结合Selenium使用。
2、Behave简介
Behave是一个用于行为驱动开发 (Behavior-Driven Development, BDD) 的 Python 库。使用 Behave,可以编写自然语言格式的使用场景来描述软件的行为,然后用 Python 实现这些场景下的步骤,形成可直接运行的测试。
Behave提供了一种将业务逻辑和测试逻辑分离的方法,因此它可以使测试代码更加可读,可维护和可重用。
3、behave测试步骤
1、编写场景文件。在“features”的目录下创建后缀名为.feature文件,描述测试情景。
2、实现场景步骤。在steps的目录下写.py代码。
3、测试。
3、Behave的测试场景(features)
在 Behave 中,“given-when-then”是常用的测试步骤流程模式,用来描述测试场景和测试步骤,其中 given 表示给定前置条件,when 表示触发动作,then 表示验证结果。整个测试用例包含了完整的测试步骤和断言。
在目录中新建一个名为features的目录,在features中新建一个login.feature的文件,内容如下。
Feature: 登录功能
Scenario: 正常登录Given 用户名 admin密码 admin@123When 打开页面And 输入用户名And 输入密码And 点击登录按钮Then 页面中应不包含 您输入的帐号信息不正确
Behave的场景代码(steps)
在目录中创建一个 steps 目录,在该目录中创建一个名为 login.py 的文件,其中包含测试步骤的实现。
import osfrom behave import given, when, then
from selenium import webdriver
from selenium.webdriver.common.by import By@given('用户名 {username} 密码 {password}') # 对应步骤 Given 关键词 behave, 参数放在{}中
def step_impl(context, username,password): # context是上下文对象,有参数的话,加上对应参数context.username = username # 将参数绑定上下文对象,以便其他步骤使用context.password = password # 将参数绑定上下文对象,以便其他步骤使用@when('打开页面')
def step_impl1(context):option=webdriver.ChromeOptions()option.add_experimental_option('excludeSwitches', ['enable-automation']) #去掉浏览器的自动化标识option.add_argument('--no-sandbox')#解决DevToolsActivePort文件不存在的报错option.add_argument("--disable-blink-features=AutomationControlled")#禁用仅在 Chrome 由自动化控制时启用的功能driver_path ='library/chromedriver.exe'option.binary_location = "D:\\下载\\chrome-win64\\chrome.exe"driver = webdriver.Chrome(executable_path=driver_path,options=option)driver.maximize_window()driver.implicitly_wait(10)driver.get('http://localhost:8080/login')context.driver = driver
@when('输入用户名')
def step_impl2(context):context.driver.implicitly_wait(10)context.driver.find_element(By.ID, 'username').send_keys(context.username)
@when('输入密码')
def step_impl3(context):context.driver.implicitly_wait(10)context.driver.find_element(By.ID, 'password').send_keys(context.password)sleep(0.5)
@when('点击登录按钮')
def step_impl4(context):context.driver.implicitly_wait(10)context.driver.find_element(By.ID, 'login-btn').click()sleep(0.5)
@then('页面中应不包含 {msg}')
def step_impl5(context,msg):context.driver.implicitly_wait(10)assert msg not in context.driver.title
从behave中导入given,when,then等关键字,这些关键字通过注解的形式修饰每一个测试方法(例如:用 @given 装饰器定义了一个测试输入用户名和密码的步骤,传入一个参数 username和password,将其保存到 context 对象中,以便后续步骤可以使用)
对应场景的每个步骤编写一个step_impl(context)函数,上方有对应的关键字装饰器匹配对应的场景步骤
执行
命令行cmd切换到 features目录, 运行 behave 命令。测试的时候会新开一个Chrome窗口.在执行 Behave 测试用例后,Behave 会自动输出测试结果,结果包括了测试用例总数、通过数和失败数等信息。一个典型的 Behave 测试结果如下所示:
1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
4 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m3.204s
下图是控制台的截图