成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專(zhuān)欄INFORMATION COLUMN

Python Behave框架學(xué)習(xí)知識(shí)詳解

89542767 / 870人閱讀


  小編寫(xiě)這篇文章的主要目的,就是給大家詳細(xì)的講解一些關(guān)于Python Behave框架知識(shí)的一些相關(guān)解答,大家要仔細(xì)的進(jìn)行閱讀哦。


  behave是python語(yǔ)言的行為驅(qū)動(dòng)開(kāi)發(fā),全稱(chēng):Behavior-driven development,簡(jiǎn)稱(chēng)BDD。


  BDD框架


  BDD即行為驅(qū)動(dòng)開(kāi)發(fā)(Behavior Driven Development),其特點(diǎn)為:


  經(jīng)過(guò)通俗易懂的語(yǔ)言,以此用來(lái)進(jìn)行自定義相關(guān)系統(tǒng)行為


  展現(xiàn)消費(fèi)者的角度來(lái)看,需要編寫(xiě)需求場(chǎng)景


  對(duì)此,我們能做到的是,鼓勵(lì)軟件項(xiàng)目中的開(kāi)發(fā)者、非技術(shù)人員以及商業(yè)參與者之間的協(xié)作。協(xié)作的核心是通過(guò)活文檔或者說(shuō)場(chǎng)景文檔來(lái)協(xié)作,該文檔既是測(cè)試用例文檔,也是需求定義文檔


  常見(jiàn)的BDD框架有Behave,Cucumber等


  它是一種敏捷軟件開(kāi)發(fā)技術(shù),它鼓勵(lì)軟件項(xiàng)目中的開(kāi)發(fā)人員、QA和非技術(shù)或業(yè)務(wù)參與者之間進(jìn)行協(xié)作。


  python behave的官方網(wǎng)址:


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


  最初由Dan North命名,并于二零零九年對(duì)BDD給出了如下定義:


  “BDD是二代版本、由外而內(nèi)、基于拉動(dòng)、多利益相關(guān)者、多規(guī)模、高度自動(dòng)化、敏捷的方法。


  它描述了一個(gè)與定義明確的輸出交互的循環(huán),從而交付了重要的工作、測(cè)試軟件?!?/p>


  BDD并不會(huì)描述或定義軟件怎么做,而是能做了什么。最終通過(guò)python代碼進(jìn)行驗(yàn)證。


  首先用pycharm創(chuàng)建一個(gè)項(xiàng)目Python-Behave,python環(huán)境選擇Virtualenv,接著安裝behave包。


  在項(xiàng)目Python-Behave下創(chuàng)建一個(gè)名為“features”的目錄(這個(gè)目錄名稱(chēng)是隨意的),可以在這個(gè)目錄下定義所有behave的文件結(jié)構(gòu)。


  在features目錄下創(chuàng)建一個(gè)“.feature”文件,這是一種叫作“Gherkin”的語(yǔ)言。它對(duì)非技術(shù)人員比較友好,可以使用自然語(yǔ)言編寫(xiě)。


  “.feature”文件有兩個(gè)用途:文檔和自動(dòng)化測(cè)試。一句話,在“.feature”里編寫(xiě)測(cè)試場(chǎng)景。


  很多文章提到Gherkin語(yǔ)言必須用pycharm專(zhuān)業(yè)版才能編寫(xiě),但是我親測(cè)用pycharm社區(qū)版也是可以編寫(xiě)的。


  “.feature”文件的結(jié)構(gòu):


  主體由多個(gè)場(chǎng)景Scenario組成,可以選用Background和tag進(jìn)行約束。


  feature文件的一個(gè)基本的結(jié)構(gòu)為:

 

   Feature:feature name
  Scenario:some scenario
  Given some condition
  When some operation
  Then some result is expected


  Feature是功能名稱(chēng)


  Scenario是場(chǎng)景描述


  Given是此場(chǎng)景下的前提條件


  When是此場(chǎng)景下的操作步驟


  Then是此場(chǎng)景下的預(yù)期結(jié)果


  如果有多個(gè)測(cè)試場(chǎng)景呢,就再加一個(gè)Scenario。如果Scenario下的Given/When/Then有多個(gè)呢?


  可以用And或But表示。


  所以


  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


  也可以這樣寫(xiě)作


  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


  這種方式閱讀會(huì)更流暢


  當(dāng)然,上面只是一個(gè)簡(jiǎn)單的feature結(jié)構(gòu),更復(fù)雜一點(diǎn)的,比如說(shuō)這樣:


   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由一系列類(lèi)似于Scenario的步驟組成,它的目的是為Scenario添加相關(guān)的文章,在Scenario執(zhí)行之前執(zhí)行Background,用于設(shè)置Scenario的前提條件。


  腳本編寫(xiě)由一系列步驟組成,它描述了特使的一種場(chǎng)景。如果一種場(chǎng)景有多種情況呢?


  比如登錄這個(gè)Scenario,不同的登錄名和密碼,登錄的結(jié)果不同。這種情況可以不需要寫(xiě)多個(gè)Scenario描述,可以使用Scenario Outline和Examples來(lái)完成。


  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|


  在上面這個(gè)例子中,用Scenario Outline描述“Blenders”場(chǎng)景,用多個(gè)Examples表示場(chǎng)景的多種類(lèi)型,每個(gè)Examples下可以包含多種情況。不同情況的列舉在Scenario Outline用符號(hào)“<key>”表示,在Examples中用key列舉


  Scenario由一系列步驟組成,步驟由關(guān)鍵字“Given”、“When”、“Then”、“And”、“But”為開(kāi)頭。Python Behave實(shí)際運(yùn)行的也是這些步驟。


  具體實(shí)現(xiàn)是通過(guò)此項(xiàng)目下的steps目錄里的“.py”文件實(shí)現(xiàn)所有的Scenario的步驟。這里要注意,steps目錄名是確定的不能改變的,但是里面的py文件名是隨意的。


  python behave項(xiàng)目的執(zhí)行方式也并不是通過(guò)運(yùn)行steps目錄里的py文件,而是通過(guò)命名behave調(diào)用“.feature”文件,映射到py文件里的步驟下的函數(shù),執(zhí)行這些函數(shù)。


  步驟描述要盡量簡(jiǎn)潔,但有時(shí)會(huì)附帶一些文本text或表格table。如果python代碼需要使用這些text或table,則可以通過(guò)訪問(wèn)屬性“context.text”或”context.table“來(lái)使用。


  Text:


  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:
  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中訪問(wèn):
   given('a set of specific users')
  def step_impl(context):
  for row in context.table:
  model.add_user(name=row['name'],department=row['department'])


  這個(gè)表格在python中是這樣的數(shù)據(jù)類(lèi)型:


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


  認(rèn)真體會(huì)下?。?!


  Tags:


  tags用于標(biāo)記Feature、Scenario或Scenario Outlook,可以選擇性的只執(zhí)行被標(biāo)記的。


  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


  如果只想執(zhí)行Scenario:Weaker opponent,可以對(duì)它進(jìn)行標(biāo)記為 slow,然后運(yùn)行“behave--tags=slow”。


  如果想執(zhí)行除標(biāo)記 slow外的其他場(chǎng)景,可以運(yùn)行“behave--tags=“not slow””。


  組合使用tags標(biāo)簽:


  –tags=“wip or slow”,選擇所有標(biāo)記為wip或slow的case


  –tags=“wip and slow”,選擇所有標(biāo)記為wip和slow的case


  以上講的是如何用“.feature”文件編寫(xiě)所有測(cè)試場(chǎng)景。雖然是通過(guò)命令behave xxx.feature觸發(fā),但實(shí)際的執(zhí)行操作是在steps目錄下的“.py”實(shí)現(xiàn)。


  所以,要如何把“.feature”里的所有步驟映射到“.py”中,還必須按照順序不能出錯(cuò),這就成為了一個(gè)關(guān)鍵。


  假設(shè)給定一個(gè)Senario:


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


  記住,只對(duì)所有的步驟按順序?qū)崿F(xiàn),并不會(huì)對(duì)Scenario進(jìn)行映射。


  在python中實(shí)現(xiàn)如下:


   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”中的步驟的順序,將步驟的前面的關(guān)鍵字,在python中用裝飾器匹配( give、 when、 then),()里面是步驟描述。


  如果是And或But,在python中用它們被重命名以前的關(guān)鍵字。也就是說(shuō),在python中,不可能有 and或 but。


  然后在裝飾器下方定義函數(shù),函數(shù)名隨意。函數(shù)體就是步驟的具體實(shí)現(xiàn)。


  如果你想在某個(gè)步驟里執(zhí)行另一個(gè)步驟,只需要用context對(duì)象調(diào)用execute_steps()函數(shù),里面?zhèn)魅氡徽{(diào)用步驟。


   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文件中用中括號(hào)加關(guān)鍵字表示“{key}”。


  比如訪問(wèn)百度首頁(yè):


  Scenario:訪問(wèn)百度網(wǎng)頁(yè)
  Given:打開(kāi)網(wǎng)頁(yè)
  When:輸入網(wǎng)址www.baidu.com
  Then:顯示百度首頁(yè)
  關(guān)于其中輸入百度網(wǎng)址,在python中就可以這樣實(shí)現(xiàn):
   when(輸入網(wǎng)址{baidu})
  def step_imp1(context,baidu):
  context.driver.get(baidu)


  Context:


  聰明的你一定注意到了,在py文件中每個(gè)步驟下的函數(shù)內(nèi)第一個(gè)參數(shù)是context,它是Feature或Scenario的實(shí)例化,可以用來(lái)傳遞信息。


  如何傳遞呢,這里我們暫且按下不表,先思考一個(gè)問(wèn)題。


  如果我有一個(gè)場(chǎng)景,打開(kāi)百度網(wǎng)頁(yè),輸入關(guān)鍵字搜索,然后查看搜索結(jié)果。


  在feature文件中如何描述,這個(gè)我們應(yīng)該已經(jīng)學(xué)會(huì)了。在py文件中如何實(shí)現(xiàn),我們也不陌生了。


  Scenario:打開(kāi)百度網(wǎng)頁(yè)并輸入關(guān)鍵字
  Given:打開(kāi)百度網(wǎng)頁(yè)http://www.baidu.com
  When:輸入關(guān)鍵字大美女
  Then:驗(yàn)證返回的搜索結(jié)果標(biāo)題是大美女_百度搜索
   given(打開(kāi)百度網(wǎng)頁(yè){baidu})
  def step_imp1(context,baidu):
  context.driver.get(baidu)
   when(輸入關(guān)鍵字{keyword})
  def step_imp1(context,keyword):
  context.driver.find_element_by_xpath('//*[ id="kw"]').send_keys(keyword)
  ...


  后面我就不寫(xiě)了?,F(xiàn)在只看given的步驟,你應(yīng)該就能發(fā)現(xiàn)問(wèn)題,context.driver哪來(lái)的,從已知的上下文中并沒(méi)有相關(guān)信息。


  如果你再仔細(xì)的思考一下,就會(huì)發(fā)現(xiàn)這里少了一步,我們?cè)诖蜷_(kāi)百度網(wǎng)站前,是不是應(yīng)該先打開(kāi)瀏覽器,然后才是輸入百度網(wǎng)址。


  如果你有多個(gè)關(guān)于網(wǎng)頁(yè)搜索的場(chǎng)景,你是不是應(yīng)該每次執(zhí)行Scenario前都要打開(kāi)瀏覽器,執(zhí)行完畢關(guān)閉瀏覽器。這個(gè)操作類(lèi)似于python unittester中的setup()和teardown()的用法。


  那python behave框架中,對(duì)于操作前和操作后的前置條件和后置條件,是放在了“environment.py”文件中定義。


  它有以下幾種:


  before_step(context,step)和after_step(context,step),每一步之前和之后運(yùn)行


  before_scenario(context,scenario)和after_scenario(context,scenario),每個(gè)場(chǎng)景運(yùn)行之前和之后運(yùn)行


  before_feature(context,feature)和after_feature(context,feature),每個(gè)feature文件執(zhí)行之前和之后運(yùn)行


  before_tag(context,tag)和after_tag(context,tag),每個(gè)標(biāo)簽調(diào)用之前和之后運(yùn)行


  before_all(context)和after_all(context),整個(gè)behave之前和之后運(yùn)行


  那上面的例子中,在environment.py中實(shí)現(xiàn)打開(kāi)和關(guān)閉瀏覽器的操作,要這樣實(shí)現(xiàn):


  #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中用上面列舉的函數(shù)直接定義,函數(shù)名和形參必須符合規(guī)范。


  可以看出,如果environment.py中的信息、變量或?qū)ο笮枰趕teps中的py文件中被使用,可以用context來(lái)存儲(chǔ)。


  這里就把打開(kāi)的瀏覽器對(duì)象賦值給了context下定義的driver屬性,然后在py文件中直接可以使用context.driver,就相當(dāng)于使用這個(gè)瀏覽器了。


  注意注意,environment.py文件在python behave項(xiàng)目中的位置是哪里?是steps目錄中嗎?


  不是的,environment.py文件是和“.feature”文件、steps目錄并列在同一目錄下的。而且它的名稱(chēng)必須是environment.py。


  現(xiàn)在回頭看下一個(gè)完整的python behave項(xiàng)目的最低結(jié)構(gòu)要求:


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


  更復(fù)雜的目錄為:


  +--features/
  |+--steps/
  ||+--website_steps.py
  ||+--utils.py
  ||
  |+--environment.py#--Environment file with behave hooks,etc.
  |+--signup.feature
  |+--login.feature
  |+--account_details.feature


  最后,如何執(zhí)行behave的程序?


  不是執(zhí)行的steps目錄里的py文件,而是通過(guò)cmd打開(kāi)命令行窗口,執(zhí)行:“behave xxx.feature”。


  這樣也很麻煩,有沒(méi)有一種方式可以在py文件的主入口里執(zhí)行"behave xxx.feature",這樣可以封裝成exe程序,雙擊運(yùn)行exe,即可運(yùn)行behave。


  在python behave項(xiàng)目目錄下創(chuàng)建一個(gè)py文件(也就是和feature文件、steps目錄文件在同一級(jí)下),命名為main.py,然后寫(xiě)一個(gè)主入口程序:


  #main.py
  from behave.__main__ import main as behave_main
  import os
  if __name__=="__main__":
  #獲取main.py的當(dāng)前目錄
  featurefile_dir=os.path.dirname(os.path.realpath(__file__))
  #獲取feature文件的絕對(duì)路徑
  featurefile_path=os.path.join(featurefile_dir,"Behave.feature")
  #用behave.__main__下的main函數(shù),傳入feature文件路徑,實(shí)現(xiàn)behave xxx.feature相同的效果
  #但是這里有個(gè)主要點(diǎn),傳入feature文件的路徑不能是以這樣“”或這樣“\”的表示方式,必須要改成這種“/”
  featurefile_path=featurefile_path.replace("\","/")
  behave_main(featurefile_path)
  #或者你不轉(zhuǎn)成“”,那么你就不能直接傳入feature文件路徑了,必須把文件路徑放入一個(gè)list中,然后把list傳入
  #如果你傳入list,相當(dāng)于傳入多個(gè)參數(shù),這時(shí)候除了文件路徑,還可以傳入tags參數(shù),以執(zhí)行標(biāo)記的功能或場(chǎng)景
  cmd_order=[]
  cmd_order.append(featurefile_path)#把feature文件路徑添加為list的第一個(gè)元素
  cmd_order.append("-t slow")#把tag標(biāo)簽添加為list的第二個(gè)元素
  behave_main(cmd_order)#傳入?yún)?shù)列表


  到此,本篇文章就給大家寫(xiě)到這里了,希望可以為各位讀者帶來(lái)幫助。


文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/127749.html

相關(guān)文章

  • 首次公開(kāi),整理12年積累的博客收藏夾,零距離展示《收藏夾吃灰》系列博客

    摘要:時(shí)間永遠(yuǎn)都過(guò)得那么快,一晃從年注冊(cè),到現(xiàn)在已經(jīng)過(guò)去了年那些被我藏在收藏夾吃灰的文章,已經(jīng)太多了,是時(shí)候把他們整理一下了。那是因?yàn)槭詹貖A太亂,橡皮擦給設(shè)置私密了,不收拾不好看呀。 ...

    Harriet666 評(píng)論0 收藏0
  • 寫(xiě)這么多系列博客,怪不得找不到女朋友

    摘要:前提好幾周沒(méi)更新博客了,對(duì)不斷支持我博客的童鞋們說(shuō)聲抱歉了。熟悉我的人都知道我寫(xiě)博客的時(shí)間比較早,而且堅(jiān)持的時(shí)間也比較久,一直到現(xiàn)在也是一直保持著更新?tīng)顟B(tài)。 showImg(https://segmentfault.com/img/remote/1460000014076586?w=1920&h=1080); 前提 好幾周沒(méi)更新博客了,對(duì)不斷支持我博客的童鞋們說(shuō)聲:抱歉了!。自己這段時(shí)...

    JerryWangSAP 評(píng)論0 收藏0
  • 「碼個(gè)蛋」2017年200篇精選干貨集合

    摘要:讓你收獲滿(mǎn)滿(mǎn)碼個(gè)蛋從年月日推送第篇文章一年過(guò)去了已累積推文近篇文章,本文為年度精選,共計(jì)篇,按照類(lèi)別整理便于讀者主題閱讀。本篇文章是今年的最后一篇技術(shù)文章,為了讓大家在家也能好好學(xué)習(xí),特此花了幾個(gè)小時(shí)整理了這些文章。 showImg(https://segmentfault.com/img/remote/1460000013241596); 讓你收獲滿(mǎn)滿(mǎn)! 碼個(gè)蛋從2017年02月20...

    wangtdgoodluck 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<