除了通过或失败之外,Python 单元测试是否有替代结果?
Is there an alternative result for Python unit tests, other than a Pass or Fail?
我正在编写具有数据库依赖性的单元测试(因此从技术上讲它们是功能测试)。通常,这些测试不仅依赖于数据库的实时性和功能性,而且还依赖于某些数据的可用性。
例如,在一项测试中,我可能会查询数据库以检索我将用于测试更新或删除功能的示例数据。如果数据尚不存在,那么在这种情况下这不完全是失败。我只关心更新或删除的 pass/fail 状态,在这种情况下我们甚至没有进行足够的测试。所以我不想给出假阳性或假阴性。
有没有一种优雅的方法可以使单元测试 return 成为第三种可能的结果?比如警告?
总的来说,我认为 的建议最适合大多数情况:
This is a failure though- your tests failed to set up the system in
the way that your test required. Saying "the data wasn't there, so it
is okay for this test to fail" is saying that you don't really care
about whether the functionality you are testing works. Make your test
reliably insert the data immediately before retrieving it, which will
give you better insight into the state of your system.
但是,在我的特定情况下,我正在编写一个功能测试,该测试依赖于从多个进程生成和操作的数据。在测试开始时快速生成它是不切实际的(至少现在是这样)。
我最终发现我需要它来使用这里提到的 skipTest:
Skip unittest if some-condition in SetUpClass fails
我正在编写具有数据库依赖性的单元测试(因此从技术上讲它们是功能测试)。通常,这些测试不仅依赖于数据库的实时性和功能性,而且还依赖于某些数据的可用性。
例如,在一项测试中,我可能会查询数据库以检索我将用于测试更新或删除功能的示例数据。如果数据尚不存在,那么在这种情况下这不完全是失败。我只关心更新或删除的 pass/fail 状态,在这种情况下我们甚至没有进行足够的测试。所以我不想给出假阳性或假阴性。
有没有一种优雅的方法可以使单元测试 return 成为第三种可能的结果?比如警告?
总的来说,我认为
This is a failure though- your tests failed to set up the system in the way that your test required. Saying "the data wasn't there, so it is okay for this test to fail" is saying that you don't really care about whether the functionality you are testing works. Make your test reliably insert the data immediately before retrieving it, which will give you better insight into the state of your system.
但是,在我的特定情况下,我正在编写一个功能测试,该测试依赖于从多个进程生成和操作的数据。在测试开始时快速生成它是不切实际的(至少现在是这样)。
我最终发现我需要它来使用这里提到的 skipTest:
Skip unittest if some-condition in SetUpClass fails