pytest 安裝與入門

1.1 安裝pytest

  1. 命令行執行以下命令

    $ pip3 install -U pytest
    
  2. 檢查版本

    $ pytest --version
    This is pytest version 4.5.0, imported from /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pytest.py
    

1.2 創建你的第一個測試用例

  1. 創建一個簡單的測試方法

    #test_sample.py
    def func(x):
        return x + 1
    
    def test_a():
        print("---test_a----")
        assert func(3) == 5 #斷言失敗
        
    def test_b():
      	print("---test_b---")
        assert 1 #斷言成功
    
  2. 執行一下

    • 命令行模式

      • 命令下執行

        $ pytest -s test_sample.py
        
    • 主函數模式

      • 增加主函數

        if __name__ == '__main__':
        	pytest.main(["-s", "test_sample.py"])
         #-s 表示支持控制檯打印,如果不加,print 不會出現任何內容
         #-q 安靜模式,不打印信息
        
    • 執行結果

      ============================= test session starts ==============================
      platform darwin -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
      rootdir: /Users/wuyanhong/PycharmProjects/Inter_AutoTest_W, inifile: pytest.ini
      plugins: rerunfailures-7.0, ordering-0.6, metadata-1.8.0, html-1.20.0, allure-pytest-2.6.4
      collected 2 items
      
      test_sample.py ---test_a----
      F---test_b---
      .
      
      =================================== FAILURES ===================================
      ____________________________________ test_a ____________________________________
      
          def test_a():
              print("---test_a----")
      >       assert func(3) == 5  # 斷言失敗
      E       assert 4 == 5
      E        +  where 4 = func(3)
      
      test_sample.py:9: AssertionError
      ====================== 1 failed, 1 passed in 0.09 seconds ======================
      
      Process finished with exit code 0
      

      由於func(3)並不等於5,這次測試返回了一個失敗報告。

      • . 表示成功
  • 如果需要更多信息,可以使用-v或–verbose

  • F 表示失敗

    Console參數介紹
    
    - -v 用於顯示每個測試函數的執行結果
    - -q 只顯示整體測試結果
    - -s 用於顯示測試函數中print()函數輸出
    - -x, --exitfirst, exit instantly on first error or failed test
    - -h 幫助
    

1.3 執行pytest測試

  • 執行方法

  • py.test -q test_class.py
    py.test               # run all tests below current dir
    py.test test_mod.py   # run tests in module
    py.test somepath      # run all tests below somepath
    py.test -k stringexpr # only run tests with names that match the
                          # the "string expression", e.g. "MyClass and not method"
                          # will select TestMyClass.test_something
                          # but not TestMyClass.test_method_simple
    py.test test_mod.py::test_func # only run tests that match the "node ID",
    			       # e.g "test_mod.py::test_func" will select
                                   # only test_func in test_mod.py
    
  • 如何執行多條測試?

    - 測試文件以test_*.py開頭或*_test.py結尾
    - 測試類以Test開頭,並且不能帶有 __init__ 方法
    - 測試函數以test_開頭
    - 斷言使用基本的assert即可
    
    • pytest會執行當前目錄及子目錄下所有test_*.py*_test.py格式的文件

    • 可以設置pytest.ini配置文件,自定義執行文件格式

      addopts = -s 
      # 當前目錄下的scripts文件夾 -可自定義
      testpaths = testcase
      # 當前目錄下的scripts文件夾下,以test_開頭,以.py結尾的所有文件 -可自定義
      python_files = test_*.py
      # 當前目錄下的scripts文件夾下,以test_開頭,以.py結尾的所有文件中,以Test_開頭的類 -可自定義
      python_classes = Test_*
      # 當前目錄下的scripts文件夾下,以test_開頭,以.py結尾的所有文件中,以Test_開頭的類內,以test_開頭的方法 -可自定義
      python_functions = test_*
      
  • 兼容unittest.TestCase

  • - test_*.py or *_test.py files.
    - @unittest.skip style decorators;
    - setUp/tearDown;
    - setUpClass/tearDownClass;
    - setUpModule/tearDownModule;
    
發佈了25 篇原創文章 · 獲贊 1 · 訪問量 650
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章