虽然单元测试的理念是测试可以按任何顺序运行并且它应该通过,但是如果您正在实现一个没有其他与服务器通信的方式的 API 怎么办...并且您需要测试某个特定的非常基本的功能(例如删除),然后才能执行更复杂的任务?那么订购测试是否合理?
如果是这样,我该如何使用 python 的 unittest
模块来实现?
最佳答案
您似乎已经意识到您的单元测试应该是独立的。我可以看到您想以某种固定顺序运行测试的唯一其他原因是,如果早期测试失败,您希望停止运行该套件。为此,您可以使用 command-line option
-f, --failfast
Stop the test run on the first error or failure.
顺便说一下,测试是按字母顺序运行的:
the order in which the various test cases will be run is determined by sorting the test function names with respect to the built-in ordering for strings.
( docs )
关于Python 单元测试哲学和排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9637534/