gpt4 book ai didi

python - pytest:在线程中运行

转载 作者:太空狗 更新时间:2023-10-29 21:46:25 24 4
gpt4 key购买 nike

一个名为 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 通过了?

最佳答案

因为 AssertionError 在一个单独的线程中被引发。您的 test_demo_thread_false 方法不会断言任何内容,它只会生成一个新线程,而且它始终会成功执行此操作。

关于python - pytest:在线程中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23338301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com