pytest文檔2-用例運行規則


用例設計原則

  • 文件名以test_*.py文件和*_test.py
  • 以test_開頭的函數
  • 以Test開頭的類
  • 以test_開頭的方法
  • 所有的包pakege必須要有__init__.py文件

help幫助

1.查看pytest命令行參數,可以用pytest -h 或pytest --help查看


C:\Users\admin>pytest -h
usage: pytest [options] [file_or_dir] [file_or_dir] [...]

positional arguments:
  file_or_dir

general:
  -k EXPRESSION         only run tests which match the given substring
                        expression. An expression is a python evaluatable
                        expression where all names are substring-matched
                        against test names and their parent classes. Example:
                        -k 'test_method or test_other' matches all test
                        functions and classes whose name contains
                        'test_method' or 'test_other', while -k 'not
                        test_method' matches those that don't contain
                        'test_method' in their names. Additionally keywords
                        are matched to classes and functions containing extra
                        names in their 'extra_keyword_matches' set, as well as
                        functions which have names assigned directly to them.
  -m MARKEXPR           only run tests matching given mark expression.
                        example: -m 'mark1 and not mark2'.
  --markers             show markers (builtin, plugin and per-project ones).
  -x, --exitfirst       exit instantly on first error or failed test

reporting:
  -v, --verbose         increase verbosity.
  -q, --quiet           decrease verbosity.
  --verbosity=VERBOSE   set verbosity
 
只貼了一部分

按以下目錄寫用例

D:YOYO\
    __init__.py
    
    test_class.py
        #  content of  test_class.py  
        class TestClass:
            def test_one(self):
                x = "this"
                assert 'h' in x
        
            def test_two(self):
                x = "hello"
                assert hasattr(x, 'check')
                
            def test_three(self):
                a = "hello"
                b = "hello world"
                assert a in b
            
    test_sample.py
        #  content of  test_sample.py
        def func(x):
            return x +1
    
        def test_answer():
            assert func(3)==5


python -m

cmd執行pytest用例有三種方法,以下三種方法都可以,一般推薦第一個

  • pytest

  • py.test

  • python -m pytest

如果不帶參數,在某個文件夾下執行時,它會查找該文件夾下所有的符合條件的用例(查看用例設計原則)

執行用例規則

1.執行某個目錄下所有的用例

pytest 文件名/

2.執行某一個py文件下用例

pytest 腳本名稱.py

3.-k 按關鍵字匹配

pytest -k "MyClass and not method"

這將運行包含與給定字符串表達式匹配的名稱的測試,其中包括Python
使用文件名,類名和函數名作為變量的運算符。 上面的例子將運行
TestMyClass.test_something但不運行TestMyClass.test_method_simple

4.按節點運行

每個收集的測試都分配了一個唯一的nodeid,它由模塊文件名和后跟說明符組成
來自參數化的類名,函數名和參數,由:: characters分隔。

運行.py模塊里面的某個函數

pytest test_mod.py::test_func

運行.py模塊里面,測試類里面的某個方法

pytest test_mod.py::TestClass::test_method

5.標記表達式

pytest -m slow

將運行用@ pytest.mark.slow裝飾器修飾的所有測試。

6.從包里面運行

pytest --pyargs pkg.testing

這將導入pkg.testing並使用其文件系統位置來查找和運行測試。

-x 遇到錯誤時停止測試

pytest -x test_class.py

從運行結果可以看出,本來有3個用例,第二個用例失敗后就沒繼續往下執行了

D:\YOYO>pytest -x test_class.py
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: D:\YOYO, inifile:
collected 3 items

test_class.py .F

================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________

self = <YOYO.test_class.TestClass object at 0x0000000003A29780>

    def test_two(self):
        x = "hello"
>       assert hasattr(x, 'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_class.py:11: AssertionError
===================== 1 failed, 1 passed in 0.05 seconds ======================

--maxfail=num

pytest --maxfail=1

當用例錯誤個數達到指定數量時,停止測試

D:\YOYO>pytest --maxfail=1
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: D:\YOYO, inifile:
collected 4 items

test_class.py .F

================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________

self = <YOYO.test_class.TestClass object at 0x0000000003A3D080>

    def test_two(self):
        x = "hello"
>       assert hasattr(x, 'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_class.py:11: AssertionError
===================== 1 failed, 1 passed in 0.06 seconds ======================

---------------------------------pytest結合selenium自動化完整版-------------------------

全書購買地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b

作者:上海-悠悠 QQ交流群:874033608

也可以關注下我的個人公眾號:yoyoketang


免責聲明!

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



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