一个名为pytest_demo.py的python文件:
import pytest
import threading
@pytest.mark.test
class TestDemo():
def test_demo_false(self):
assert False
def test_demo_true(self):
assert True
def test_demo_thread_true(self):
thread1 = MyThread(True)
thread1.start()
def test_demo_thread_false(self):
thread1 = MyThread(False)
thread1.start()
class MyThread(threading.Thread):
def __init__(self, flag):
threading.Thread.__init__(self)
self.flag = flag
def run(self):
print "Starting "
assert self.flag
if __name__ == "__main__":
pytest.main(['-v', '-m', 'test', 'pytest_demo.py'])
输出是在运行后" python pytest_demo.py":
pytest_demo.py:8: TestDemo.test_demo_false FAILED
pytest_demo.py:11: TestDemo.test_demo_true PASSED
pytest_demo.py:14: TestDemo.test_demo_thread_true PASSED
pytest_demo.py:18: TestDemo.test_demo_thread_false PASSED
在线程中,为什么TestDemo.test_demo_thread_false被传递?
答案 0 :(得分:6)
因为AssertionError是在一个单独的线程中引发的。
你的test_demo_thread_false
方法没有断言任何东西,它只是产生一个新的线程,它总是成功地做到了。