Pytest-file-execution

提供:Dev Guides
移動先:案内検索

Pytest-ファイル実行

この章では、単一のテストファイルと複数のテストファイルを実行する方法を学習します。 テストファイル test_square.py が既に作成されています。 次のコードで新しいテストファイル test_compare.py を作成します-

def test_greater():
   num = 100
   assert num > 100

def test_greater_equal():
   num = 100
   assert num >= 100

def test_less():
   num = 100
   assert num < 200

今、すべてのファイル(ここでは2つのファイル)からすべてのテストを実行するには、次のコマンドを実行する必要があります-

pytest -v

上記のコマンドは、 test_square.pytest_compare.py の両方からテストを実行します。 出力は次のように生成されます-

test_compare.py::test_greater FAILED
test_compare.py::test_greater_equal PASSED
test_compare.py::test_less PASSED
test_square.py::test_sqrt PASSED
test_square.py::testsquare FAILED
================================================ FAILURES
================================================
______________________________________________ test_greater
______________________________________________
   def test_greater():
   num = 100
>  assert num > 100
E  assert 100 > 100

test_compare.py:3: AssertionError
_______________________________________________ testsquare
_______________________________________________
   def testsquare():
   num = 7
>  assert 7*7 == 40
E  assert (7 * 7) == 40

test_square.py:9: AssertionError
=================================== 2 failed, 3 passed in 0.07 seconds
===================================

特定のファイルからテストを実行するには、次の構文を使用します-

pytest <filename> -v

今、次のコマンドを実行します-

pytest test_compare.py -v

上記のコマンドは、ファイル* test_compare.py。*からのみテストを実行します。結果は-

test_compare.py::test_greater FAILED
test_compare.py::test_greater_equal PASSED
test_compare.py::test_less PASSED
============================================== FAILURES
==============================================
____________________________________________ test_greater
____________________________________________
   def test_greater():
   num = 100
>  assert num > 100
E  assert 100 > 100
test_compare.py:3: AssertionError
================================= 1 failed, 2 passed in 0.04 seconds
=================================