pytest 利用 configtest.py 定制自己的運行范圍 並重跑失敗的case


    先來說一下conftest.py ,這個文件放在不同的路徑下,pytest 會根據層級關系來確定其作用范圍,官方建議放在項目根目錄下,不宜路徑太深。

pytest 在啟動后會加載配置文件,例如 ini 文件,和這個 conftest.py。

pytest 命令行參數會被傳遞給 config變量,這是一個全局對象,類似的還有 request, session,items。可以參考 pytest官網的reference。

So,如果在命令行中給出 “--lf” ,那么這個值將會在 config.option.lf 屬性被設置成bool True;如果在命令行中給出了特定的測試用例function,這個值將會在 config.args 列表中出現。這兩點接下來會用到。

    首先設計第一個,這個測試環境,是stage環境還是dev 環境,兩者使用不用的測試數據,比如登錄用戶名,這個值在command line中設置

# content of project man.py
import pytest
if __name__ == '__main__':
  pytest.main(['-v','--envopt=stage']) # content of conftest.py class CaseEnv(): def __init__(self): self.TestURI = None self.AdminUser = None self.AdminUserPWD = None def pytest_addoption(parser): # set testing environment to stage or devasis parser.addoption( "--envopt", action="store", default="stage", help="my option: stage or devasia" #將會在 config.option 中增加 devopt屬性,默認值stage ) @pytest.fixture(scope='session') #級別session可以被class級別的fixture使用 def xyyopt(request): # request is a pytest fixture, envused = request.config.getoption("--envopt") TestData = CaseEnv() if envused == 'stage': TestData.TestURI = 'https://app.net.cn' TestData.AdminUser = 'admin666' TestData.AdminUserPWD = 'password123' else: print 'Now is not stage environment !!! ' return TestData #在其他使用這個fixture的function中 通過 xyyopt.TestURI 就可以得到 webdriver初始化用的測試網址

 在command line 中增加的option,需要送給一個地方接收它,它就是config對象。所以用到了 pytest_addoption(parser)這個hook。隨后根據它來確定自定義fixture的返回值,以作后續使用。

 

    接下來,又希望自定義哪些測試用例被執行。用到了 nodeid,它是一個測試function在pytest中的呈現方式,結構類似於這樣 “Test_module.py::Test_class::test_function_1”;還用到了另一個hook,pytest_configure(config)。將這些用例 按照pytest的格式列在某一個配置文件中,修改這個列表就可以實現只跑這些用例。

    同樣,修改conftest.py 文件:

def pytest_configure(config):

      casesids = MyCaseList.get('caseids')   # A list of cases ids I want to run  MyCaseList 是我的case 列表文件
      config.args = casesids       # add cases to config just like doing in commond line

 這樣,不需要修改command line,就可以划定case的范圍,並且,會根據這個case 列表的索引順序從【0】來執行。

 

  最后,還想要把fail的case再跑一遍,但是為來避免產生的 --junit-xml 結果會覆蓋先前的記過,so,對於 --lf 的結果單獨寫到另一個 xml 中,目前的簡單方式是

#content of project main
import pytest
if __name__ == '__main__':
    pytest.main(['-v','--envopt=stage'])
    pytest.main(['-v','--lf'])     #use it to run previous failed case


#modify conftest.py/pytest_configure(config)

def pytest_configure(config):
    ...
    ...
    ...
    if config.getoption('lf') is True:             # if command line have --lf option, save run last fail report to different xml file
    config.option.xmlpath = './report-stage-lf.xml' else: config.option.xmlpath = './report-stage.xml' # specify --junite-xml report file path .. .. ..

 這樣在對應路徑下將會有兩個測試結果xml文件:report-stage.xml,第一次運行自定義好的用例的結果;report-stage-lf.xml,存放了上一次fail的case第二次運行后的結果。寫在不同xml文件中的原因是,第二次的--lf 運行會在原來的xml中覆蓋結果。

更好一點的辦法是,安裝pytest-html 第三方plugin,只需要將 pytest.main(['-v','--envopt=stage','--html=./alltest_result.html']) 增加一個pytest-html 的option “--html” 就可以實現兩個分開的 .html 結果了,美觀易讀。

安裝方式 $pip install pytest-html,更多pytest-html 可選項,參考github吧。

config.option 的屬性值在另一個地方記錄了下來 https://www.cnblogs.com/xiaoyanxuzi/p/10510722.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM