python-按不同类别标记单元测试:单元,集成,“ dangerous_integration”

时间:2019-04-06 21:03:17

标签: python unit-testing

问题是,有没有一种方法可以按不同类别标记单元测试:单元,集成,“ dangerous_integration”。然后从“ Team City”设置中自动跳过“ dangerous_integration”?

对于标记为“危险”的测试-不应自动运行。这些需要手动运行,最好甚至在非常仔细的调试器上运行?

对于python,这是一个示例,其中我拥有一个可以将实时交易发送到加密货币交易所的类。我基本上将所有测试方法都以“返回”作为测试方法的第一行,以免发生我们无意间将实盘交易发送到街上的情况。

import unittest
class BinanaceConnectorTests(unittest.TestCase):
    def setUp(self):
        ... 

    def tearDown(self):
        ...

    def testSendRealOrders(self):
        return <-- I hardcode return here so nobody accidentally 
        ... actual implementation shunted by above return ...

这是正确的方法吗?我正在使用Visual Studio。我记得在C#中使用xunit时,可以通过不同的类别标记单元测试。

我认为没有“ dangerous_integration”测试之类的东西-如果您需要这样的东西,请将这些测试分离到手动python脚本中,而根本就不要将它们标记为任何类型的单元测试。那是最安全的。

1 个答案:

答案 0 :(得分:0)

The scenarios for selective test execution that you have presented (like dangerous_integration) should probably better be addressed in a different way, as was already discussed in the comments. There are, however, situations where you want certain tests to be excluded in the normal case. For example, you might have some long-running tests which you only want to execute occasionally. Or, some tests produce outputs beyond simple 'passed' or 'failed' results that require thorough analysis, which you also only want to run at times.

One approach can be to group these tests into classes of their own and use this to include or exclude them from the respective runs. Or, if it seems more appropriate to have these tests implemented together with other tests, you could use some naming convention that indicates to which category a test belongs. You have the option to selectively run tests that are grouped or named in certain ways as is described in the Python unittest manual: https://docs.python.org/3.7/library/unittest.html. For example, the attribute testNamePatterns of unittest.TestLoader could be used for the selection of tests based on their names.