pytest:テストケースの作成-テストケースの収集-テストケースの実行-テストレポートの生成
テストケースを書く:
ユースケース名、ユースケースステップ、期待される結果、実際の結果、前と後
1、ユースケース名:test_
2で始まり、assert assert :(実際と期待される比較)assert式(True / False)
テストケースの自動収集:
1。テストケースの収集用のディレクトリ:rootdirをルートディレクトリとして使用します。rootdirディレクトリからユースケースの検索を開始します。
2.ディレクトリでのファイルフィルタリング:ファイル名がtest_で始まるpyファイル、またはファイル名が_testで終わるpyファイル。
3.ファイルでのユースケースフィルタリング:.pyで始まる関数、.pyで始まるクラスの関数名はtest_ / methodsで始まり(Testで始まる)、メソッド名はtest_で始まります
例:test_case01.py
from random import randint
def random_num():
return randint(1,10)
def test_1():
print("第一条测试用例")
assert random_num() == 2
def test_2():
print("第二条测试用例")
assert random_num() == 4
def test_3():
print("第三条测试用例")
assert random_num() == 6
def test_4():
print("第四条测试用例")
assert random_num() == 8
def test_5():
print("第五条测试用例")
assert random_num() == 10
def test_6():
print("第六条测试用例")
assert random_num() == 1
ルートディレクトリに、現在の名前:test.pyなどのpyファイルを作成します。
注:test.pyファイルを実行すると、現在のルートディレクトリでtest_で始まるファイルが自動的に検索され、pyファイルでテストケースが実行され、テストレポートが出力されます。
import pytest
pytest.main(["-s","-v","--html=测试报告.html","--alluredir=allure-report-files"])
出力されるhtmlテストレポートは次のとおりです。
html report --html plugin
1. pip install pytest-
html2.pytestコマンドにパラメーターを追加します。--html=report path(rootdirに対して))
-------------------------------------------------- -------------------------------------------------- ---------------------------
テストケースファイルは、pycharmコンソールから実行できます。
1. [ターミナル]を選択し、現在のファイルディレクトリを入力します
2.コンソールに次のように入力します:pytest
出力結果は次のとおりです。
(現在の実行結果:5回の失敗、1回の成功)