单元测试测试顺序 [英] Unittest tests order

查看:97
本文介绍了单元测试测试顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何确定单元测试方法的顺序?字母或数字前缀是否正确?

How can I be sure of the unittest methods' order? Is the alphabetical or numeric prefixes the proper way?

class TestFoo(TestCase):
    def test_1(self):
        ...
    def test_2(self):
        ...

class TestFoo(TestCase):
    def test_a(self):
        ...
    def test_b(self):
        ...

推荐答案

您可以通过设置 sortTestMethodsUsing 到无:

You can disable it by setting sortTestMethodsUsing to None:

import unittest
unittest.TestLoader.sortTestMethodsUsing = None

对于纯单元测试,你们是对的;但是对于组件测试和集成测试...我不同意你对国家不承担任何责任.如果您正在测试状态怎么办?

For pure unit tests, you folks are right; but for component tests and integration tests... I do not agree that you shall assume nothing about the state. What if you are testing the state?

例如,您的测试验证服务在安装时自动启动.如果在您的设置中,您启动服务,然后执行断言,然后您不再测试状态,而是测试服务启动"功能.

For example, your test validates that a service is auto-started upon installation. If in your setup, you start the service, then do the assertion, and then you are no longer testing the state, but you are testing the "service start" functionality.

另一个例子是当您的设置需要很长时间或需要大量空间并且频繁运行设置变得不切实际时.

Another example is when your setup takes a long time or requires a lot of space and it just becomes impractical to run the setup frequently.

许多开发人员倾向于使用单元测试"组件测试框架...所以停下来问问自己,我是在做单元测试还是组件测试?

Many developers tend to use "unit test" frameworks for component testing...so stop and ask yourself, am I doing unit testing or component testing?

这篇关于单元测试测试顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆