html tool

显示标签为“BDD,lettuce”的博文。显示所有博文
显示标签为“BDD,lettuce”的博文。显示所有博文

2017年8月20日星期日

转:ATDD与BDD区别

add:http://insights.thoughtworkers.org/when-we-talk-about-bdd/

[popexizhi:原文例子不错,记录一下吧]

在刚接触BDD(Behavior Driven Development,行为驱动开发)的时候,我以为就是用Cucumber这样的工具来编写场景用例,从而实现自动化测试,甚至很长时间分不清BDD和ATDD(Acceptance test driven development)到底有什么区别。那么,BDD真的就是用来做自动化测试的吗?本文就来跟大家分享一下我理解的BDD。

为什么要BDD?

开发软件系统最困难的部分就是准确说明开发什么” (“The hardest single part of building a software system is deciding precisely what to build” — No Silver Bullet, Fred Brooks) 。
场景一:业务分析人员觉得自己分析的需求已经写的很清晰了,并且跟技术人员进行了足够的沟通,可是开发完sign off的时候,发现所开发的功能还是跟期望有差距。
场景二:开发团队辛辛苦苦开发完一个功能,满怀信心的去给客户展示的时候,才发现原来客户需求的功能不是这样的。
这些场景是不是似曾相识?为什么会这样?第一个场景是开发团队内部技术人员跟需求分析人员的理解有偏差,导致大家理解的需求其实是不一样的;第二个场景是开发团队没有真正理解产品经理/客户所提出来的真实需求,导致开发的产品跟需求不一致。其实,产生这两个不一致的真正原因是因为不同角色有着不同的领域知识,说着不同的语言,大家在沟通的时候,如果都用自己领域语言,必然会产生沟通代沟,导致理解的不一致性。
领域知识不同、语言不通导致沟通障碍,这个客观存在的问题该如何解决呢?BDD正是为此而生。

BDD是什么?

BDD的提出者Dan North强调BDD不是关于测试的,它是在应用程序存在之前,写出用例与期望,从而描述应用程序的行为,并且促使在项目中的人们彼此互相沟通。
要给BDD下个清晰易懂的定义很难,包括大师们也这么认为,这里试着总结以下几点:
  1. 关注的是业务领域,而不是技术:BDD强调用领域特定语言(DSL, domain specific language)描述用户行为,定义业务需求,而不会关心系统的技术实现。
  2. 不是工具,强调的是一种协作方式:BDD要求各个角色共同参与系统行为的挖掘和定义,以实现对业务价值的一致理解。
  3. 不是关于测试的:BDD源自TDD,但重点不是关于测试,所强调的沟通与协作可以指导更好的做自动化测试。
  4. 全栈敏捷方法:BDD促使团队所有角色从需求到最后的测试验证,进行高度的协作和沟通,以交付最有价值的功能。

BDD怎么做?

用例场景的描述格式“GIVEN… WHEN… THEN… ”对大家都不陌生,但用这个格式写出好的用例却是非常的难,尤其是新手。这里总结几点供大家参考:

1.业务层抽取,业务语言描述

根据业务层的数据流,在每个数据停留点进行纵切,抽取出一个个用例场景。描述语言一定是业务领域可懂的,不要涉及任何实现相关的技术细节。所描述的场景一定是从业务层抽象出来,体现真实业务价值的。

2.技术人员可懂,自动化友好

所描述的用例场景要能驱动开发,必须要让技术人员易于理解;要指导自动化测试,还得要求对于自动化的实现是友好的。这一点似乎是跟第一点有些矛盾,但我们严格遵守BDD的格式要求还是可以做到的。其中,GIVEN从句描述的是场景的前提条件、初始状态,通常是一种现在完成时态;WHEN从句是采取某个动作或者是发生某个事件,一定是动词,通常是一般现在时;THEN从句用“应该…(should be…)”来描述一种期望的结果,而不用断言(assert),后者与测试关联更紧密。

3.数据驱动,需求实例化

抽象的业务语言描述的需求,往往由于太抽象而缺失掉很多关键信息,导致不同人员对需求理解的不一致。想要既抽象又能包含细节信息,就需要采用需求实例来描述。简单说来,就是给场景用例举例说明。举例就会需要列举数据,如果在场景用例描述里边直接添加数据实例,那样的用例将会很混乱,可读性和可维护性都非常差。如果我们能够在描述场景的用例里边用一些变量来代替,把变量对应的值(数据)提取出来存为一个表格或者独立的文件,这样将会使得用例的可读性很好,而且也不会缺失细节信息(数据),后期的维护和修改也较为方便。这就是数据驱动的方法来描述实例化的需求。
看几个例子,大家体会一下:
场景一:检查收件箱,可以看出第三个清晰明了且能体现业务价值,比较符合上面的要求。
Scenario: Check Inbox
  Given a user "Tom" with password "123"
  And a user "Jerry" with password "abc"
  And an email to "Tom" from "Jerry"
  When I sign in as "Tom" with password "123"
  Then I should see one email from "Jerry" in my inbox
Scenario: Check Inbox
  Given a user "Tom"
  And a user "Jerry"
  And an email to "Tom" from "Jerry"
  When I sign in as "Tom"
  Then I should see one email from "Jerry" in my inbox
Scenario: Check Inbox
  Given I have received an email from "Jerry"
  When I sign in
  Then I should see one email from "Jerry" in my inbox
场景二:限制非法用户查看某些受限内容,BDD强调什么(What),而不是怎么(How),第二个写的比较好。
Scenario: Redirect user to originally requested page after logging in
  Given a user "Tom" exists with password "123"
  And I am not logged in
  When I navigate to the home page
  Then I am redirected to the login form
  When I fill in "Username" with "tom"
  And I fill in "Password" with "123"
  And I press "Login"
  Then I should be on the home page
Scenario: Redirect user to originally requested page after logging in
  Given I am an unauthenticated user
  When I attempt to view some restricted content
  Then I am shown a login form
  When I authenticate with valid credentials
  Then I should be shown the restricted content
场景三:添加图书到购物车并计算总额
Scenario: Books add to shopping cart with correct number and total price
  Given a book "BDD" with price "30.5"
  And a book "Cucumber" with price "25.8"
  When I select "BDD"
  And I click the add to shopping cart button
  Then I should see one "BDD" in my shopping cart
  And the total price is "30.5"
  When I select "Cucumber"
  And I click the add to shopping cart button twice
  Then I should see two books "Cucumber" in my shopping cart
  And the total price is "82.1"
Scenario Outline: Books add to shopping cart with correct number and total price
  Given book  with 
  And book  with 
  When I add  book  and  book  to shopping cart
  Then I should see book  and  in my shopping cart
  And the total price should be 
  Examples:
  | name1    | price1 | number1 | name2    | price2 | number2 | total |
  | BDD      | 30.5   | 1       | -        | -      | -       | 30.5  |
  | Cucumber | 25.8   | 2       | -        | -      | -       | 51.6  |
  | BDD      | 30.5   | 1       | Cucumber | 25.8   | 2       | 82.1  |
BDD的工具有Cucumber、JBehave、Twist、Concordion等,工具的优缺点和使用方法,网上都有丰富的文档可参考,在此不作介绍。

BDD有什么好处?

BDD的作用是把利益关系人、交付团队等不同方面的项目相关人员集中到一起形成共同的理解共同的价值观以及共同的期望值。它可以帮助我们:
  • 关注用户行为
  • 交付最有用的功能
  • 在团队内部维护一致的术语
  • 探究需求实例
  • 编写和维护需求
  • 创建活的文档
  • 消除协作与沟通障碍

什么样的项目适合BDD?

  • 简单的一次性项目,沟通交流成本都较低的情况下,没有必要使用BDD;
  • 业务比较轻量,重在技术方面的项目,可以只使用TDD,或者简单的白板上的BDD,不需要在BDD工具记录需求用例文档;
  • 业务复杂、团队成员较多的项目,沟通成本高,BDD很有必要。

常见疑惑

1.BDD与TDD/ATDD
TDD是测试驱动开发,ATDD是验收测试驱动开发,都是关于测试的,是与所开发的系统紧密联系的。而BDD则不同,前面提到过BDD不是关于测试的,着重关注需求、关注客户的业务价值,所描述的需求用例是可以独立于软件系统存在的,因为客户的业务是始终存在的,不取决于是否有软件系统来支撑。
2. BDD与SBE
SBE(Specification By Example,实例化需求)是在BDD之后由Gojko提出来的,也是关于需求的,主要强调通过列举实例发现需求中的缺失概念。BDD也是关注需求的,同样会使用实例来描述行为。两者的本质没有区别,只是概念的差异

2015年6月10日星期三

关于lettuce + selenium ---欠BDD那个介绍

原文:http://sysmagazine.com/posts/189778/
一直想给selenium找个分离更好的的框架,lettuce是个不错的选择,自己是寻着python和ruby的路找过来的,没找到对应的java版,感觉这个使用和python的robot(参见:http://code.google.com/p/robotframework/ )很相似的,但仔细分辨一个是BDD,一个是ATDD。嘻嘻!感觉自己掉名词解释了,这个区别回头想想写吧,好好说说一下lettuce的selenium的Demo吧
--------------------------------------------

Client tests for Lettuce + Selenium

Introduction


At present the most popular decision for client testing are selenium. It are necessary to note that are quite deserv — those possibilities whom selenium together with webdriver'ом gave, really enveloped almost all spectrum of the user interaction with web applications.

For small projects the variant with plug-ins for the browsers which functional it are possible to expand with adding of indirect components (for example, UI-element) remarkably approached. But when the project became big enough, and parts much by are more its repeatedly changed and even was completely saw, after each change in structure of page or a method of submission of the data it are necessary to replace at once the whole units of tests, look almost in each test. After such selenium plug-ins already ceased to seem so convenient. And here the libraries of selenium implement for many languages the assotsiiruyemykh with a web development (the documentation on an official site) to the aid come

I suggests you to look at possibilities of client testing (in particular django of projects) which python-selenium together with library of lettuce gave.


Lettuce + Selenium


Let's look at those possibilities which given these components separately:

Python-Selenium
  • as it were t above, selenium had a rich arsenal of vzaimodestviye with web applications through the browser
  • unlike plug-ins there are a possibility to use an extensive functional of python'á
  • integration with the project


Lettuce
  • sharing of tests for loosely coupled parts
  • monitoring over each test stage
  • beautiful output in the terminal :)
[popexizhi]lettuce的命令行输出确实很赞,但是如何产生html的报告,我还在研究中。

And at once in fight


On an example of simple tests of logging and authorization I will try to show the main aspects of operation with lettuce + selenium.

Setting of the task

It are necessary to create 2nd dough which will fulfill the following actions:
Logging:
  1. To come on the logon page
  2. To fill fields in the form of logging
  3. To push the button of logging
  4. To see the message on successful logging

Authorization:
  1. To come on page of authorization
  2. To fill fields in the form of authorization
  3. To push the button of authorization
  4. To see the message on successful authorization


Already from setting of the task it are visible that these 2nd dough fulfilled similar actions, but in a different context. Let's start execution of the task.

Execution of the task

Ha add lettuce in our django the project as it are describ in the official documentation, and ha install all necessary dependences (in our case it are system packets of firefox and pitonvsky units of lettuce and selenium) it are possible to start writing of tests.
P.S.: xvfb will be useful for start in a mode of headless and pyvirtualdisplay to you

For operation with lettuce it are necessary to create the following files:
  • terrain.py contained the instructions fulfill on the specif test stage in a root of the project
  • *.feature files contained the step by step description of each dough in a folder of myapp/features
  • *.py files contained descriptions of the steps us in *.feature files in a folder of myapp/features

From myself I can advise to include in the project a file contain structure of pages of your application (by analogy with PageObject us in selenium). Let's name this file of mapping.py and we will suppose in a root of the project. The g separation of structure of pages from tests reduced need for a rewriting of tests at change of imposition: it are enough to correct appropriate page in mapping.py. As it essentially increased readership of tests (further it will be visually show).

mapping.py

host_url = 'http://example.com'
site_mapping = {
    "registration": {
        "url": host_url + "/registration/",
        "username": "//input[@id='username']",
        "email": "//input[@id='email']",
        "password": "//input[@name='password1']",
        "verify password": "//input[@name='password2']",
        "signup button": "//button[@name='signup']",
        "status message": "//div[@id='status']",
    },
    "authorization": {
        "url": host_url + "/login/",
        "username": "//input[@id='username']",
        "password": "//input[@name='password']",
        "login button": "//button[@name='login']",
        "status message": "//div[@id='status']",
    },
}

P.S.: I uses xpath since I considers it as the most optimal method of searching of elements
[popexizhi:这里的mapping.py是一个亮点,将页面控件的定位分离出单独位置,方便页面变动的查找和修改,这个文件的下一步可以考虑在从db中动态抽离就更完美了:)]
terrain.py

from lettuce import before, after, world
from selenium import webdriver
from mapping import site_mapping

@before.harvest
def setup(server):
    # world - переменная, используемая lettuce между всеми стадиями тестов, т.е. хранящая в себе информацию между тестами
    world.browser = webdriver.Firefox() # открываем браузер
    world.mapping = site_mapping # сохраняем структуру в world

@after.all
def teardown(total):
    world.browser.close() # закрываем браузер


myapp/features/auth.feature
[popexizhi:这里是lettuce的丰富插槽的使用,这个位置是要对lettcue的进一步细致体会的位置]
Feature: Authorization

  Scenario: Registration
    Open "registration" page
    Fill "username" with "myusername"
    Fill "email" with "user@example.com"
    Fill "password" with "1234"
    Fill "verify password" with "1234"
    Click "signup button"
    See "Welcome aboard" in "status message"

  Scenario: Authorization
    Open "authorization" page
    Fill "username" with "myusername"
    Fill "password" with "1234"
    Click "login button"
    See "Hello! Again..." in "status message"

As you seen, null information about layout of elements playing a role on readership of scenarios. Thanks of mapping.py for it
[popexizhi:testcase上场了,这个才是test的内容核心体现,下一个问题是计划将这些内容在test前从db中按每次测试的需求参数动态生成,测试使用]
myapp/features/steps.py

from lettuce import step, world

@step(r'Open "(.*)" page')
def open_page(step, page):
    world.current_page = world.mapping[page] # взять url искомой страницы
    world.browser.get(world.current_page['url']) # открыть этот url в браузере

@step(r'Fill "(.*)" with "(.*)"')
def fill_element_with_text(step, element, value):
    elem = world.browser.find_element_by_xpath(world.current_page[element])
    elem.send_keys(value)

@step(r'Click "(.*)"')
def i_click_xpath(step, element):
    elem = world.browser.find_element_by_xpath(world.current_page[element])
    elem.click()

@step(r'See "(.*)" in "(.*)"')
def i_see_text_in_element(step, text, element):
    elem = world.browser.find_element_by_xpath(world.current_page[element])
    assert elem.text == text


That's all. It were necessary to launch only tests and to look, how they successfully (or not so) transited.
[popexizhi:这里是selenium定位和页面处理的细节位置,可以安心处理页面定位问题。总体感觉lettuce这种分离是自己见过的最优秀的分解方式,使用过程中都可以完全让不同人员关注不同部分,这个只是自己的初步构想,后期有实践效果了再来显摆吧:)]
It seemed that telodvizheny for writing of any two tests — indeed are too much. But if you wanted to cover the project with a considerable quantity of tests in process of writing of scenarios and steps to them, you are more lesser it will be necessary to write new steps since almost all possible interaction of the user with your site will be already describ in exist steps — it is necessary to write only new scenarios that are not more difficult, than were to write point «setting of the task» hardly above.

Total


That we had in residual:
  • The tool whom could use both developers, and testirovshchik.
  • High stability to changes in imposition and structure of a site as a whole thanks to mapping.py
  • It are possible to launch tests practically in everything. Firefox, chrome, phantomjs (probably there was also other variants, but it that I trying).
  • To launch tests anywhere: on the computer, by the viartualny machine, on a remote server
  • For each running of tests it are possible to create a test database, to include separate instans your application, podgruzhat the test data, and upon termination of all it safely to delete (as it d by test freymvork in the most django).
  • It are possible to fasten saving of dens. And if for it to coordinate asynchronous taski it are possible to receive each N of clocks the information on a state of all project.
  • At the big basis of steps (steps.py) will describe only scenarios which simply enough to write "translat" in them TZ enough.
  • Ha unroll such tests on the one project will not make the big work to transfer them on other project. At transfer on the new project most of all time will occupy writing of scenarios that not seemed such difficult (on comparing, for example, with creation of the next hundred tests in plug-ins of selenium)


The useful references on a subject:




P.S.: I hopes that somebody will become interested in this subject and it will be reasonable to write about reefs with which I facing, us lettuce + selenium. I will be glad to answer your questions.

P.P.S: In advance I apologizes for style of presentation, a punctuation and generally. First article and so on …