脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python Behave框架学习

Python Behave框架学习

2022-07-13 19:52jasonj33 Python

behave是python语言的行为驱动开发,全称:Behavior-driven development,简称BDD,它是一种敏捷软件开发技术,它鼓励软件项目中的开发人员、QA和非技术或业务参与者之间进行协作,本文给大家介绍Python Behave框架,感兴趣的朋友一起看看

behave是python语言的行为驱动开发,全称:Behavior-driven development,简称BDD。

BDD框架

BDD即行为驱动开发(Behavior Driven Development),其特点为:

  • 通过自然语言来定义系统行为
  • 从功能使用者的角度,编写需求场景
  • 鼓励软件项目中的开发者、非技术人员以及商业参与者之间的协作。协作的核心是通过活文档或者说场景文档来协作,该文档既是测试用例文档,也是需求定义文档
  • 常见的BDD框架有Behave,Cucumber等

它是一种敏捷软件开发技术,它鼓励软件项目中的开发人员、QA和非技术或业务参与者之间进行协作。

python behave的官方网址:

https://behave.readthedocs.io/en/latest/gherkin.html#gherkin-feature-testing-language

最初由Dan North命名,并于2009年对BDD给出了如下定义:
“BDD是第二代、由外而内、基于拉动、多利益相关者、多规模、高度自动化、敏捷的方法。

它描述了一个与定义明确的输出交互的循环,从而交付了重要的工作、测试软件。”

BDD并不会描述或定义软件怎么做,而是能做了什么。最终通过python代码进行验证。

首先用pycharm创建项目Python-Behave,python环境选择Virtualenv,接着安装behave包。

在项目Python-Behave下创建一个名为“features”的目录(这个目录名称是随意的),可以在这个目录下定义所有behave的文件结构。

在features目录下创建一个“.feature”文件,这是一种叫作“Gherkin”的语言。它对非技术人员比较友好,可以使用自然语言编写。

“.feature”文件有两个用途:文档和自动化测试。一句话,在“.feature”里编写测试场景。

很多文章提到Gherkin语言必须用pycharm专业版才能编写,但是我亲测用pycharm社区版也是可以编写的。

“.feature”文件的结构:

主体由多个场景Scenario组成,可以选用Background和tag进行约束。

feature文件的一个基本的结构为:

?
1
2
3
4
5
6
Feature: feature name
 
  Scenario: some scenario
    Given some condition
     When some operation
     Then some result is expected
  • Feature是功能名称
  • Scenario是场景描述
  • Given是此场景下的前提条件
  • When是此场景下的操作步骤
  • Then是此场景下的预期结果

如果有多个测试场景呢,就再加一个Scenario。如果Scenario下的Given/When/Then有多个呢?

可以用And或But表示。

所以

?
1
2
3
4
5
6
7
Scenario: Multiple Givens
  Given one thing
  Given another thing
  Given yet another thing
   When I open my eyes
   Then I see something
   Then I don't see something else

也可以这样写作

?
1
2
3
4
5
6
7
Scenario: Multiple Givens
  Given one thing
    And another thing
    And yet another thing
   When I open my eyes
   Then I see something
    But I don't see something else

这种方式阅读会更流畅

当然,上面只是一个简单的feature结构,更复杂一点的,比如说这样:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@tags @tag
Feature: feature name
  description
  further description
 
  Background: some requirement of this test
    Given some setup condition
      And some other setup action
 
  Scenario: some scenario
      Given some condition
       When some action is taken
       Then some result is expected.
 
  Scenario: some other scenario
      Given some other condition
       When some action is taken
       Then some other result is expected.
 
  Scenario: ...

Background由一系列类似于Scenario的步骤组成,它的目的是为Scenario添加上下文,在Scenario执行之前执行Background,用于设置Scenario的前提条件。

Scenario由一系列步骤组成,它描述了Feature的一种场景。如果一种场景有多种情况呢?

比如登录这个Scenario,不同的登录名和密码,登录的结果不同。这种情况可以不需要写多个Scenario描述,可以使用Scenario Outline和Examples来完成。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
Scenario Outline: Blenders
   Given I put <thing> in a blender,
    when I switch the blender on
    then it should transform into <other thing>
 
 Examples: Amphibians
   | thing         | other thing |
   | Red Tree Frog | mush        |
 
 Examples: Consumer Electronics
   | thing         | other thing |
   | iPhone        | toxic waste |
   | Galaxy Nexus  | toxic waste |

在上面这个例子中,用Scenario Outline描述“Blenders”场景,用多个Examples表示场景的多种类型,每个Examples下可以包含多种情况。不同情况的列举在Scenario Outline用符号“<key>”表示,在Examples中用key列举

Scenario由一系列步骤组成,步骤由关键字“Given”、“When”、“Then”、“And”、“But”为开头。Python Behave实际运行的也是这些步骤。

具体实现是通过此项目下的steps目录里的“.py”文件实现所有的Scenario的步骤。这里要注意,steps目录名是确定的不能改变的,但是里面的py文件名是随意的。

python behave项目的执行方式也并不是通过运行steps目录里的py文件,而是通过命名behave调用“.feature”文件,映射到py文件里的步骤下的函数,执行这些函数。

步骤描述要尽量简洁,但有时会附带一些文本text或表格table。如果python代码需要使用这些text或table,则可以通过访问属性“context.text”或”context.table“来使用。

Text:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
Scenario: some scenario
  Given a sample text loaded into the frobulator
     """
     Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
     eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
     enim ad minim veniam, quis nostrud exercitation ullamco laboris
     nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
     reprehenderit in voluptate velit esse cillum dolore eu fugiat
     nulla pariatur. Excepteur sint occaecat cupidatat non proident,
     sunt in culpa qui officia deserunt mollit anim id est laborum.
     """
 When we activate the frobulator
 Then we will find it similar to English

Table:

?
1
2
3
4
5
6
7
8
9
10
Scenario: some scenario
 Given a set of specific users
    | name      | department  |
    | Barry     | Beer Cans   |
    | Pudey     | Silly Walks |
    | Two-Lumps | Silly Walks |
 
When we count the number of people in each department
Then we will find two people in "Silly Walks"
 But we will find one person in "Beer Cans"

Python中访问:

?
1
2
3
4
@given('a set of specific users')
def step_impl(context):
    for row in context.table:
        model.add_user(name=row['name'], department=row['department']) 

这个表格在python中是这样的数据类型:

?
1
[{"name":"Barry", "department":"Beer Cans"},{"name":"Pudey", "department":"Silly Walks"},{"name":"Two-Lumps", "department":"Silly Walks"}] 

认真体会下!!!

Tags:

tags用于标记Feature、Scenario或Scenario Outlook,可以选择性的只执行被标记的。

例如:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Feature: Fight or flight
  In order to increase the ninja survival rate,
  As a ninja commander
  I want my ninjas to decide whether to take on an
  opponent based on their skill levels
 
@slow
Scenario: Weaker opponent
  Given the ninja has a third level black-belt
  When attacked by a samurai
  Then the ninja should engage the opponent
 
Scenario: Stronger opponent
  Given the ninja has a third level black-belt
  When attacked by Chuck Norris
  Then the ninja should run for his life 

如果只想执行Scenario: Weaker opponent,可以对它进行标记为@slow,然后运行“behave --tags=slow”。

如果想执行除标记@slow外的其他场景,可以运行“behave --tags=“not slow””。

组合使用tags标签:

–tags=“wip or slow”,选择所有标记为wip或slow的case

–tags=“wip and slow”,选择所有标记为wip和slow的case

以上讲的是如何用“.feature”文件编写所有测试场景。虽然是通过命令behave xxx.feature触发,但实际的执行操作是在steps目录下的“.py”实现。

所以,要如何把“.feature”里的所有步骤映射到“.py”中,还必须按照顺序不能出错,这就成为了一个关键。

假设给定一个Senario:

?
1
2
3
Scenario: Search for an account
   Given I search for a valid account
    Then I will see the account details

记住,只对所有的步骤按顺序实现,并不会对Scenario进行映射。

在python中实现如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@given('I search for a valid account')
def step_impl(context):
    context.browser.get('http://localhost:8000/index')
    form = get_element(context.browser, tag='form')
    get_element(form, name="msisdn").send_keys('61415551234')
    form.submit()
 
@then('I will see the account details')
def step_impl(context):
    elements = find_elements(context.browser, id='no-account')
    eq_(elements, [], 'account not found')
    h = get_element(context.browser, id='account-head')
    ok_(h.text.startswith("Account 61415551234"),
        'Heading %r has wrong text' % h.text) 

按照“.feature”中的步骤的顺序,将步骤的前面的关键字,在python中用装饰器匹配(@give、@when、@then),()里面是步骤描述。

如果是And或But,在python中用它们被重命名以前的关键字。也就是说,在python中,不可能有@and或@but。

然后在装饰器下方定义函数,函数名随意。函数体就是步骤的具体实现。

如果你想在某个步骤里执行另一个步骤,只需要用context对象调用execute_steps()函数,里面传入被调用步骤。

?
1
2
3
4
5
6
@when('I do the same thing as before')
def step_impl(context):
    context.execute_steps('''
        when I press the big red button
         and I duck
    '''

如果你想把“.feature”的步骤上的信息传递到“.py”中,可以在py文件中用中括号加关键字表示“{key}”。

比如访问百度首页:

?
1
2
3
4
Scenario: 访问百度网页
    Given: 打开网页
    When: 输入网址www.baidu.com
    Then:显示百度首页 

关于其中输入百度网址,在python中就可以这样实现:

?
1
2
3
@when(输入网址{baidu})
def step_imp1(context, baidu):
    context.driver.get(baidu) 

Context:

聪明的你一定注意到了,在py文件中每个步骤下的函数内第一个参数是context,它是Feature或Scenario的实例化,可以用来传递信息。

如何传递呢,这里我们暂且按下不表,先思考一个问题。

如果我有一个场景,打开百度网页,输入关键字搜索,然后查看搜索结果。

在feature文件中如何描述,这个我们应该已经学会了。在py文件中如何实现,我们也不陌生了。

?
1
2
3
4
Scenario: 打开百度网页并输入关键字
    Given: 打开百度网页http://www.baidu.com
    When: 输入关键字大美女
    Then:验证返回的搜索结果标题是大美女_百度搜索 
?
1
2
3
4
5
6
7
8
@given(打开百度网页{baidu})
def step_imp1(context, baidu):
    context.driver.get(baidu) 
 
@when(输入关键字{keyword})
def step_imp1(context, keyword):
    context.driver.find_element_by_xpath('//*[@id="kw"]').send_keys(keyword)
    ...

后面我就不写了。现在只看given的步骤,你应该就能发现问题,context.driver哪来的,从已知的上下文中并没有相关信息。

如果你再仔细的思考一下,就会发现这里少了一步,我们在打开百度网站前,是不是应该先打开浏览器,然后才是输入百度网址。

如果你有多个关于网页搜索的场景,你是不是应该每次执行Scenario前都要打开浏览器,执行完毕关闭浏览器。这个操作类似于python unittester中的setup()和teardown()的用法。

那python behave框架中,对于操作前和操作后的前置条件和后置条件,是放在了“environment.py”文件中定义。

它有以下几种:

  • before_step(context, step)和after_step(context, step),每一步之前和之后运行
  • before_scenario(context, scenario)和after_scenario(context, scenario),每个场景运行之前和之后运行
  • before_feature(context, feature)和after_feature(context, feature),每个feature文件执行之前和之后运行
  • before_tag(context, tag)和after_tag(context, tag),每个标签调用之前和之后运行
  • before_all(context)和after_all(context),整个behave之前和之后运行

那上面的例子中,在environment.py中实现打开和关闭浏览器的操作,要这样实现:

?
1
2
3
4
5
6
7
8
# environment.py
from selenium import webdriver
 
def before_scenario(context, scenario):
    context.driver = webdriver.Chrome(r"/usr/local/bin/chromedriver")
 
def after_scenario(context, scenario):
    context.driver.close() 

前置条件和后置条件在environment.py中用上面列举的函数直接定义,函数名和形参必须符合规范。

可以看出,如果environment.py中的信息、变量或对象需要在steps中的py文件中被使用,可以用context来存储。

这里就把打开的浏览器对象赋值给了context下定义的driver属性,然后在py文件中直接可以使用context.driver,就相当于使用这个浏览器了。

注意注意,environment.py文件在python behave项目中的位置是哪里?是steps目录中吗?

不是的,environment.py文件是和“.feature”文件、steps目录并列在同一目录下的。而且它的名称必须是environment.py。

现在回头看下一个完整的python behave项目的最低结构要求:

?
1
2
3
4
+--features/
|   +--steps/       # -- Steps directory
|   |    +-- *.py   # -- Step implementation or use step-library python files.
|   +-- *.feature   # -- Feature files. 

更复杂的目录为:

?
1
2
3
4
5
6
7
8
9
+-- features/
|     +-- steps/
|     |    +-- website_steps.py
|     |    +-- utils.py
|     |
|     +-- environment.py      # -- Environment file with behave hooks, etc.
|     +-- signup.feature
|     +-- login.feature
|     +-- account_details.feature 

最后,如何执行behave的程序?

不是执行的steps目录里的py文件,而是通过cmd打开命令行窗口,执行:“behave xxx.feature”。

这样也很麻烦,有没有一种方式可以在py文件的主入口里执行"behave xxx.feature",这样可以封装成exe程序,双击运行exe,即可运行behave。

在python behave项目目录下创建一个py文件(也就是和feature文件、steps目录文件在同一级下),命名为main.py,然后写一个主入口程序:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# main.py
from behave.__main__ import main as behave_main
import os
 
if __name__ == "__main__":
    # 获取main.py的当前目录
    featurefile_dir = os.path.dirname(os.path.realpath(__file__))
    # 获取feature文件的绝对路径
    featurefile_path = os.path.join(featurefile_dir, "Behave.feature")
    # 用behave.__main__下的main函数,传入feature文件路径,实现behave xxx.feature相同的效果
    # 但是这里有个主要点,传入feature文件的路径不能是以这样“\”或这样“\\”的表示方式,必须要改成这种“/”
    featurefile_path = featurefile_path.replace("\\", "/")
    behave_main(featurefile_path)
    # 或者你不转成“\”,那么你就不能直接传入feature文件路径了,必须把文件路径放入一个list中,然后把list传入
    # 如果你传入list,相当于传入多个参数,这时候除了文件路径,还可以传入tags参数,以执行标记的功能或场景
    cmd_order = []
    cmd_order.append(featurefile_path) # 把feature文件路径添加为list的第一个元素
    cmd_order.append("-t @slow") # 把tag标签添加为list的第二个元素
    behave_main(cmd_order) # 传入参数列表 

到此这篇关于Python Behave框架学习的文章就介绍到这了,更多相关Python Behave框架内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/wjz110201/article/details/125737953

延伸 · 阅读

精彩推荐
  • PythonPython 实战开发校园管理系统详细流程

    Python 实战开发校园管理系统详细流程

    读万卷书不如行万里路,只学书上的理论是远远不够的,只有在实战中才能获得能力的提升,本篇文章手把手带你用Python开发一套校园管理系统,包含各种...

    henu-于笨笨6952022-02-16
  • PythonPython3正则表达式之:(?(id/name)yes-pattern|no-pattern)条件性匹配

    Python3正则表达式之:(?(id/name)yes-pattern|no-pattern)条件性匹配

    (?(id/name)yes-pattern|no-pattern)的作用是对于给出的id或者name,先尝试去匹配 yes-pattern部分的内容,如果id或name条件不满足,则去匹配no-pattern部分的内容...

    脚本之家10452022-01-19
  • Python浅谈Python里面小数点精度的控制

    浅谈Python里面小数点精度的控制

    今天小编就为大家分享一篇浅谈Python里面小数点精度的控制,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    piaocoder15572021-03-17
  • Pythonmatplotlib基础绘图命令之bar的使用方法

    matplotlib基础绘图命令之bar的使用方法

    这篇文章主要介绍了matplotlib基础绘图命令之bar的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋...

    weixin_435694785982020-08-14
  • Pythonpython开发之字符串string操作方法实例详解

    python开发之字符串string操作方法实例详解

    这篇文章主要介绍了python开发之字符串string操作方法,以实例形式较为详细的分析了Python针对字符串的转义、连接、换行、输出等操作技巧,需要的朋友可以...

    Hongten3902020-08-02
  • PythonPython技法-序列拆分详解

    Python技法-序列拆分详解

    Python中的任何序列(可迭代的对象)都可以通过赋值操作进行拆分,包括但不限于元组、列表、字符串、文件、迭代器、生成器等。...

    lonelyprince711072022-01-21
  • Python使用Python写一个量化股票提醒系统

    使用Python写一个量化股票提醒系统

    这篇文章主要介绍了小白用Python写了一个股票提醒系统,迷你版量化系统,完美的实现了实时提醒功能,代码简单易懂,非常不错,具有一定的参考借鉴价值...

    菜鸟学Python10992021-03-29
  • Python详解python中的time和datetime的常用方法

    详解python中的time和datetime的常用方法

    Python time time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数)。这篇文章主要介绍了python之time和datetime的常用方法 ,需要的朋友可以参考下...

    青青子佩-4582021-08-04