当前位置: 首页 > 工具软件 > Lettuce BDD > 使用案例 >

自动化测试框架之Lettuce行为驱动

仲孙文乐
2023-12-01

1、什么是行为驱动?

        行为驱动开发,Behavior-Driven Development,简写BDD

        在软件工程中,BDD是一种敏捷软件开发的技术

2、Lettuce框架原理是什么?

        Lettuce是基于Python语言的行为驱动测试框架

        Lettuce将测试用例和自动化测试代码分离

        Lettuce写测试用例就像写文本一样清晰,也就是说它把所有的这种行为全定义好了

        (行为驱动是一种概念或理念是以行动这件事情来驱动测试框架、来驱动测试用例)

3、行为驱动的实现?

        行为驱动测试实例

        features文件夹包含test.feature文件,test.feature类似于unittest里面TestSuite定义的套件,test.feature里面可以实现具体的测试用例,每个Scenario对应一个场景就是测试用例。

# test.feature文件内容
Feature: Calculation  # 计算
    Input two number, the compare result
    
    Scenario: Do a simple add method      # 场景:做一个简单加法
        Given I have two number: 1 and 5  # 假如:有两个数1和5
        When Do add method                # 当执行加法时候
        Then I get result: 6              # 然后我会得到结果6
        
    Scenario: Do a simple add method
        Given I have two number: 2 and 5
        When Do add method
        Then I get result: 7

# 运行测试脚本,直接在features文件夹上一级路径下输入lettuce回车执行
行为驱动 % lettuce

 在features文件夹下steps.py脚本中实现每一条语句对应的方法

# encoding: utf-8
from lettuce import *
from appium import webdriver

@before.each_scenario    # 和unittest中的SetUp作用完全一样
def lauchApp(scenario):
    desired_caps = {}
        desired_caps['platformName'] = "Android"
        desired_caps['platformVersion'] = "5.1"
        desired_caps['deviceName'] = "192.168.56.102:55555"
        desired_caps['appPackage'] = "com.android.calculator2"
        desired_caps['appActivity'] = '.Calculator'
        desired_caps['unicodeKeyboard'] = "True"
        desired_caps['resetKeyboard'] = "True"
        world.driver = webdriver.Remote('http://localhost:4721/wd/hub', \
                      desired_caps)

@after.each_scenario    # 和unittest中的TearDown作用完全一样
def closeApp(scenario):
    # 使用world传递实例是行为驱动它定义一个东西和self作用是完全一样的只不过换了一个名
    world.driver.quit()   

@step("I have two number: (\d+) and (\d+)")
def have2number(step, number1, number2):
    world.number1 = number1
    world.number2 = number2

@step("Do add method")
def doAdd(step):
    numa = world.driver.find_element_by_id("digit_"+world.number1)
    numa.click()
    add = world.driver.find_element_by_id("op_add")
    add.click()
    numb = world.driver.find_element_by_id("digit_"+world.number2)
    numb.click()
    equal = world.driver.find_element_by_id("eq")
    equal.click()
    world.result = world.driver.find_element_by_calss_name(
        "android.widget.EditTest").text

@step("I get result: (\d+)")
def judgeResult(step, result):
    assert result == world.result, "The result are not equal %s and %s" % \
                     result and world.result

 类似资料: