我是 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"
我有两个问题:
1)上面的代码片段打印“测试套件通过”,而我希望在第二个测试用例失败时引发异常。
2)基本上,我想要一个pytest结果的句柄,通过它我可以知道通过和失败的测试用例的数量。这显示在测试摘要中。但是我正在寻找一个可以在执行测试后在脚本中进一步使用的对象