在python中运行unittests\integration测试

Mr *_* T. 2 python django integration-testing pytest

我有一个包含多个应用程序的Django项目.每个应用程序都有一组单元测试.我正在使用pytest作为我的测试运行器.我们已经到了一个点,我们想开始编写集成测试.我想知道是否有任何方法可以保持命名约定,从而保持pytest的自动发现,但仍然能够(通过标志?)运行不同的测试类型.想到的最直观的解决方案是测试方法甚至TestCase类的某种装饰器(类似于JUnit中的Category).
就像是:

@testtype('unittest')
def test_my_test(self):
    # do some testing

@testtype('integration')
def test_my_integration_test(self):
    # do some integration testing
Run Code Online (Sandbox Code Playgroud)

然后我可以运行测试,如:

py.test --type=integration
py.test --type=unittest
Run Code Online (Sandbox Code Playgroud)

有这样的事吗?
如果没有,我能想到的唯一其他解决方案是添加一个django命令并"手动"构建一个测试套件并使用pytest运行它......我宁愿不使用此选项.还有其他解决方案可以帮助我吗?
谢谢

Ala*_*air 8

您可以标记测试功能.

import pytest

@pytest.mark.unittest
def test_my_test(self):
    # do some testing

@pytest.mark.integration
def test_my_integration_test(self):
    # do some integration testing
Run Code Online (Sandbox Code Playgroud)

必须在pytest.ini文件中注册这些自定义标记.

然后使用该-m标志运行标记的测试

py.test -v -m unittest
Run Code Online (Sandbox Code Playgroud)

另一种选择是将测试分成unittestintegration目录.然后,您可以在特定目录中运行测试:

py.test -v unittest
Run Code Online (Sandbox Code Playgroud)

要么

py.test -v integration
Run Code Online (Sandbox Code Playgroud)