Pytestのpythonは、テストコードを書いて実行するためのフレームワークをテストに基づいています。pytestは主にAPIをテストするために使用するだけでなく、いくつかの複雑なテスト、およびデータベースやUIなどの他のテストを行うことができます。
Pytest利点:
- Pytestは、複数の試験を並列テストスイートの実行時間を短縮するために行うこと[章VIII]
- Pytestは自動的に、テストファイルやテスト機能を検出することができる必要が明示的に言及されていない[章]
- テスト_ *。pyのファイル
- * _Test.pyファイル
- 機能テスト*
- Pytestは、テストスイートの実行全体の部分集合[第三、第四、第五および第六章]、私たちは、動作中、いくつかのテストをスキップすることができ
- 習得が容易Pytest自由とオープンソース、
(A)pytestインストール
インストールpytest: PIP3はpytestインストール
:インストールが成功した場合pytestを参照してください PIP3ショーpytest
正常にインストールされた場合は、以下に表示されます
名前:pytest
バージョン:5.3.5
概要:pytest:Pythonの持つ単純な強力なテスト
ホーム・ページ:https://docs.pytest.org/en/latest/
著者:ホルガーKrekel、ブルーノ・オリベイラ、ロニーPfannschmidt、フロリスBruynooghe、ブリアナLaugher、フロリアンBruhinなど
著者-メール:なし
ライセンス:MITライセンス
場所:/usr/local/lib/python3.7/site-packages
必要なもの:pluggy、PY、のimportlibメタデータ、より-itertools、wcwidth、attrsに、梱包
必要な-によって:
(B)Pytestテストファイル/機能
2.1 Pytestテストファイル。:Pytestコマンドは自動的にすべてのカレントディレクトリに実行され、「_test.py」文書[1]、「テスト_ * PYを。」 自動デフォルトこれらのファイルはテストファイルです。もちろん、我々はまた、テストファイルやディレクトリはあなたがテストを実行したいことを示す表示することができます。
ファイル名に言及せずpytestを実行すると、現在のディレクトリとサブディレクトリにフォーマットテスト_ *。pyのか* _test.pyのすべてのファイルを実行します。Pytestは、自動的にテストファイルとしてこれらのファイルを識別します。私たちは、pytestが明示的に言及することにより、他のファイル名を実行することができます。
pytest a.py
pytest DIRNAME
2.2 Pytestテスト機能:. Pytest機能のニーズがない始まりは自動的pytestを無視されます「テスト」に「試験」名、関数から始まるテストする、pytestを明示的にテスト関数指定されたファイルとして指定することができない、その関数をテストする必要があります「試験」で始まります。
Pytestはテストで開始するテスト関数名が必要です。pytestによるテスト機能として考慮されていない*形式のテストではない関数名。私たちは、明示的にpytestは、テスト関数としてのテストを開始していない任意の機能を考慮することはできません。
[実施例1]は次のようにPytestフォルダを作成し、新しいファイルtest_nn.pyとtest_greater.pyとtest.py.ディレクトリ構造があります
Pytest
| - test.py
| - test_great.py:test_greater(); test_greater_equal(); test_less();
| - test_nn.py:testx(); test_great_haha();
Pytest実行コマンド、及びディスプレイのみtest_great.py test_nn.pyファイル、[1] test_great.pyは、以下のようにコードであり、
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
デフtest_greater(): NUM = 100 アサート NUM> 100 デフ)(test_greater_equal: NUM = 100 アサート NUM> = 100 デフtest_less(): NUM = 100 アサート NUM <200
、その結果を図2に示すように実行pytestとpytest -vコマンドがより示し、左-v
(C)Pytestを選択フルテストスイート(サブセット)のサブセットをテストすることができ
3.1。テストのサブセット(サブストリングに基づいて実行するテストを選択選択したテスト名のマッチング)をファイル名の一致に基づいてモードを
pytest -k <サブ> -v
以下のための[実施例1]
すべてのテストに大きなと(<ストリング>の項目に対応した「偉大な」)pytest -k偉大-vコマンド、
注: pytest -k「サブ」-v:テスト機能は、の「サブ」で、現在のディレクトリに一致するすべてのファイルを識別することができます。
3.2。標識された 試験(に基づいて、実行するテストグループを選択し、選択したテストグループマーカー S 適用されます)
関連パッケージを追加します。import pytest
マーカーの挿入:。@ pytest.mark <markname> ランテストをラベル:pytest -m <markname> -v
[例子2] 带标记 (marker) 的 test_mark.py 文件如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import pytest @pytest.mark.great def test_greater(): num = 100 assert num > 100 @pytest.mark.great def test_greater_equal(): num = 100 assert num >= 100 @pytest.mark.others def test_less(): num = 100 assert num < 200
(四)Pytest 通过 Fixture 和 conftest.py 加载测试所需调用
@pytest.fixture
Fixture 是测试的输入,测试被执行前, 涉及到的 fixture 会被执行到。Pytest 提供了 @pytest.fixture 将该部分代码自动附加到测试函数中
【注意】(1) 如果不加 @pytest.fixture 的话,这部分被测试函数调用的代码不会加载进入测试函数,pytest运行时会报错 ...
(2) @pytest.fixture 的作用域是当前文件,在其他文件中调用该 fixture 对应的函数会报错.We cannot use that fixture in another test file
Fixtures are functions, which will run before each test function to which it is applied. Fixtures are used to feed some data to the tests such as database connections, URLs to test and some sort of input data. Therefore, instead of runnng the same code for every test, we can attach fixture to the tests and it will run and return the data to the test before executing each test.
【例子3.1】创建 test_div.py 内容如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import pytest @pytest.mark.great def test_greater(): num = 100 assert num > 100 @pytest.mark.great def test_greater_equal(): num = 100 assert num >= 100 @pytest.mark.others def test_less(): num = 100 assert num < 200
为了克服(2)的限制, 在多个测试文件中调用 Fixture, 我们需要 conftest.py 统一定义存储 Fixture
【例子3.2】新建 conftest.py 内容如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import pytest @pytest.fixture def input_value(): input = 39 return input
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# content of file 'test_14' def test_divisible_14(input_value): assert input_value%14 ==0 # content of file 'test_15' def test_divisible_15(input_value): assert input_value%15 ==0 # content of file 'test_16' def test_divisible_16(input_value): assert input_value%16 ==0
运行 pytest -k divisible -v 时,由于conftest.py 文件已声明 fixture, 所以input_value() 函数会自动加载到每次调用之前,测试正常进行
(五)Pytest指定测试,验证实际输出和期望输出是否一致
Pytest 用 @pytest.mark.parametrize() 测试指定的<input, output>对
【例子4】添加@pytest.mark.parametrize()子句如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import pytest @pytest.mark.parametrize("num,output",[(1,11),(2,22),(3,35),(4,44)]) def test_multiplication_11(num,output): assert 11*num == output
(六)Pytest 跳过某些测试/屏蔽某些测试错
@pytest.mark.skip 用来掉过某些测试
@pytest.mark.xfail 用来屏蔽某些测试的错误
【例子5】新建 test_op.py 文件,内容如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import pytest @pytest.mark.xfail @pytest.mark.great def test_greater(): num = 100 assert num > 100 @pytest.mark.great def test_greater_equal(): num = 100 assert num >= 200 @pytest.mark.skip @pytest.mark.others def test_divisible(input_value): assert input_value < 200
运行 pytest test_op.py 运行结果如下,test_greater() 错误被屏蔽,test_greater_equal()正常报错,test_divisible()被测试跳过
(七)Pytest测试停止
cmd: pytest --maxfail = <num>
当检测出的错误个数等于num时,pytest 测试结束,不在对后面的代码进行测试
(八)Pytest 并行测试
pip install pytest-xdist // 安装pytest 并行测试工具
pytest -n <num> // runs in <num> workers
(九)Pytest 将测试结果记录到 xml 文件中
pytest -v --junitxml=<filename>
【例子6】 pytest -v --junitxml="result.xml"
参考(Reference)