Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 等待断言变为真,并超时?_Python_Python Unittest - Fatal编程技术网

Python 等待断言变为真,并超时?

Python 等待断言变为真,并超时?,python,python-unittest,Python,Python Unittest,我正在测试对时间敏感的代码,其中一个条件可能会在启动测试后的一段时间内发生,这取决于目标的性能。在我的例子中,我正在运行一个VM并等待一个工件被上传。有没有更好的方法来实现这个测试 def test_something_happened(): run_something() expected = True for i in range(20): n = verify_something() if n == expected: break time.

我正在测试对时间敏感的代码,其中一个条件可能会在启动测试后的一段时间内发生,这取决于目标的性能。在我的例子中,我正在运行一个VM并等待一个工件被上传。有没有更好的方法来实现这个测试

def test_something_happened():
  run_something()
  expected = True
  for i in range(20):
    n = verify_something()
    if n == expected:
      break
    time.sleep(10.0)
  self.assertTrue(n, expected)

这不是单元测试;这是一个集成测试。单元测试应该是确定性的,并且只依赖于您的代码,而不依赖于外部资源。@chepner您是对的-我的意思是
python单元测试
,而不是
单元测试
。我的问题是关于库和相关模式的使用,而不是关于最佳TDD实践。让我们看看您判断某个异常的方式。您希望创建一个立即引发该异常的模拟,而不是等待真正的事件实际超时。在本例中,您对实际上传不感兴趣,只对代码对感知超时的反应感兴趣。