我是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结果的引用,通过它我可以知道有多少测试用例通过和失败。这会显示在测试摘要中。但我正在寻找一个对象,在测试执行后可以进一步在脚本中使用。