【发布时间】:2018-11-28 19:23:25
【问题描述】:
我目前正在使用 pytest 来测试现有的(unittest test-suite per the documentation)。我目前正在编写一个等待分配 IP 地址然后将其返回给回调函数的线程,并且我正在编写单元测试来配合它。
这是我编写的测试用例类。
class TestGetIpAddressOnNewThread(unittest.TestCase):
def test_get_existing_ip(self):
def func(ip):
assert ip == "192.168.0.1" # Not the real IP
# Even when I introduce an assert statement that should fail, test still passes
assert ip == "shouldn't be the ip"
ip_check = GetInstanceIpThread(instance, func)
ip_check.start()
ip_check.join()
if __name__ == '__main__':
pytest.main()
这是GetInstanceIpThread 伪定义:
class GetInstanceIpThread(threading.Thread):
def __init__(self, instance, callback):
threading.Thread.__init__(self)
self.event = threading.Event()
self.instance = instance
self.callback = callback
def run(self):
self.instance.wait_until_running()
ip = self.instance.ip_address
self.callback(ip)
当我使用pytest path_to_file.py::TestGetIpAddressOnNewThread 运行这个测试用例时,它通过了(耶!)但即使我引入了应该 100% 失败的断言语句(嘘!)。出了什么问题,我该如何编写实际上失败的测试?
【问题讨论】:
标签: python multithreading testing pytest python-multithreading