除了Pass或Fail之外,Python单元测试是否有替代结果?

时间:2016-08-30 13:33:16

标签: python unit-testing functional-testing

我正在编写具有数据库依赖性的单元测试(因此从技术上讲,它们是功能测试)。通常,这些测试不仅依赖于数据库的实时性和功能性,而且还可以依赖某些数据。

例如,在一个测试中,我可能会查询数据库以检索我将用于测试更新或删除功能的示例数据。如果数据尚未存在,那么在这种情况下,这并非完全失败。我只关心更新或删除的通过/失败状态,在这种情况下,我们甚至没有足够的时间来测试它。因此,我不想给出误报或漏报。

是否有一种优雅的方法让单元测试返回第三个可能的结果?比如警告?

1 个答案:

答案 0 :(得分:1)

In general I think the advice by Paul Becotte is best for most cases:

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.

However, in my particular case, I am writing a functional test that relies on data generated and manipulated from several processes. Generating it quickly at the beginning of the test just isn't practical (at least yet).

What I ultimately found to work as I need it to use skipTest as mentioned here:

Skip unittest if some-condition in SetUpClass fails