私はPythonが初めてで、テストにpytestを使用しています
Pythonスクリプト内からpytestを実行しています。テストの結果に基づいて変更するスクリプトにグローバル変数があります。更新されたグローバル変数は、テストの実行後に再び使用されます。
import pytest
global test_suite_passed
test_suite_passed = True
def test_toggle():
global test_suite_passed
a = True
b = True
c = True if a == b else False
test_suite_passed = c
assert c
def test_switch():
global test_suite_passed
one = True
two = False
three = True if one == two else False
if test_suite_passed:
test_suite_passed = three
assert three
if __name__ == '__main__':
pytest.main()
if not test_suite_passed:
raise Exception("Test suite failed")
print "Test suite passed"
2 つの質問があります。
1) 上記のコード スニペットは「テスト スイートに合格しました」と出力しますが、2 番目のテスト ケースが失敗したため、例外が発生することを期待しています。
2) 基本的に、合格したテストケースと失敗したテストケースの数を知ることができる pytest の結果へのハンドルが必要です。これは、テストの概要に表示されます。しかし、テストの実行後にスクリプトでさらに使用できるオブジェクトを探しています