python中pytest収集用例規則と運転指定用例詳細


前言
前篇記事は、pytestがcmdで各種のコマンドラインパラメータと組み合わせてどのようにテスト用例を実行し、私たちが見たい情報を出力しているかを理解したと思います。じゃ、今日はpytestについて説明します。私達が書いた用例をどのように収集しますか?私達はまたどのような方式で単独の用例や大量運行用例を実行しますか?以下はみんなのために一つ一つ解答します!
pytest収集用例原理分析
まず、私達は下記のようなディレクトリ構造で私達のプロジェクトを新規作成します。

[pyttest         ]
|[      1]
| |__init__.py
| |test_    1.py
| |test_    2.py
|[      2]
| |__init__.py
| |test_    1.py
| |    .py
|test_    .py
|    2.py 
コードの例

# test_    1.py
def test_testFunc1():
print('
! in test_testFunc1') assert 1 == 1 def func1(): print(' ') assert 1 == 1 # test_ 2.py class TestClass1(object): def test_class_func1(self): print('
in test_class_func1') assert 1 == 1 def class_func1(self): print(' !') # test_ 1.py class TestClass2(object): def test_class_func2(self): print('
in test_class_func2',) assert 1 == 1 def class_func2(self): print(' !') def test_testFunc2(): print('
in test_testFunc2!') assert 1 == 1 def func2(): print(' ') assert 1 == 1 # .py def test_testFunc3(): print('
! in .py') assert 1 == 1 def func3(): print(' ') assert 1 == 1 # test_ 3.py def test_testFunc4(): print('
! in test_testFunc4') assert 1 == 1 def func4(): print(' ') assert 1 == 1 class TestClass3(object): def test_class_func3(self): print('
in test_class_func3') assert 1 == 1 def class_func3(self): print(' !') # 2.py def test_testFunc5(): print('
! in test_testFunc5') assert 1 == 1 def func5(): print(' ') assert 1 == 1
ここでcmd命令を使ってこのプロジェクトを実行してみます。どれぐらいの用例が有効ですか?cmdを開いて項目に切り替えるルートディレクトリ実行コマンドpytest -v

D:\pytest        >pytest -v
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest        , inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 6 items

test_    3.py::test_testFunc4 PASSED [ 16%]
test_    3.py::TestClass3::test_class_func3 PASSED [ 33%]
      1/test_    1.py::test_testFunc1 PASSED [ 50%]
      1/test_    2.py::TestClass1::test_class_func1 PASSED [ 66%]
      2/test_    1.py::TestClass2::test_class_func2 PASSED [ 83%]
      2/test_    1.py::test_testFunc2 PASSED [100%]
========================== 6 passed in 0.59 seconds ===========================
運行結果は全部で6つの用例パスがあります。そして詳しくはどの6つが挙げられていますか?私達が作成した用例によって実は6つだけではないです。なぜ6つだけ運行されていますか?以上のコード構造を総合して、私達の実行結果と比較して、このような法則を発見できるはずです。
pytetsは私達の現在運行しているディレクトリからすべてのディレクトリを検索して、test_で検索します。最初のファイルは、そしてファイルのすべてをtest_とします。冒頭の関数とTestで始まるクラスとクラスの中でtest_最初の関数はテストケースです。これはなぜ上のテスト用の6つの例を実行しましたか?
pytest運転指定試験用例
上記のプロジェクトを使ってデモ(cdmをプロジェクトのルートディレクトリに切り替え)を行います。
1.運転指定ディレクトリ下のすべての用例
私たちはテスト用のカタログ1を実行するすべての用例を指定します。(pytest-vテスト用のカタログ1)

D:\pytest        >pytest -v       1
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest        , inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 2 items
      1/test_    1.py::test_testFunc1 PASSED [ 50%]
      1/test_    2.py::TestClass1::test_class_func1 PASSED [100%]

========================== 2 passed in 0.05 seconds ===========================
#                     
2.実行指定ファイルのすべての用例
私たちは運行test_を指定します。テストモジュール1.py(pytest-vテスト用のカタログ1/test_テストモジュール1.py)

D:\pytest        >pytest -v       1/test_    1.py
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest        , inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 1 item
      1/test_    1.py::test_testFunc1 PASSED [100%]

========================== 1 passed in 0.09 seconds ===========================
#             
3.指定ファイルのテストクラスを実行します。
私たちは運行test_を指定します。テストモジュール2.pyのテストクラスTestclass 1(pytest-vテスト用のカタログ1/test_テストモジュール2.py:TestClass 1)

D:\pytest        >pytest -v       1/test_    2.py::TestClass1
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest        , inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 1 item
      1/test_    2.py::TestClass1::test_class_func1 PASSED [100%]

========================== 1 passed in 0.05 seconds ===========================
#                
4.指定された試験用例関数を実行します。
私たちは運行test_を指定します。test Funnc 1(pytest-vテスト用ディレクトリ1/test_テストモジュール1.py:test_testFnc 1)

D:\pytest        >pytest -v       1/test_    1.py::test_testFunc1
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest        , inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 1 item
      1/test_    1.py::test_testFunc1 PASSED [100%]

========================== 1 passed in 0.03 seconds ===========================
締め括りをつける
収集用例規則:検索はすべてtest_で行います。先頭のテストファイルは、Test先頭のテストクラスで、test_最初のテスト関数
実行用例規則:vパラメータから出力された実行情報は、指定されたディレクトリを実行するための用例としてコマンドpytestディレクトリ/ディレクトリを使用すれば良いことが分かります。実行指定ファイルはpytestディレクトリ/ファイルを使用すればいいです。指定されたクラスまたは関数を実行します。コマンドpytestディレクトリ/ファイル:クラス名:関数名またはpytestディレクトリ/ファイル:関数名
検索用例規則も私たちが用例ファイルと命名し、テストクラス、テスト関数のルールです。指定されたテストケースを実行して規則を覚えておけばいいです。
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。