在python中运行unittests \ integration测试

时间:2016-05-26 14:57:43

标签: 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

然后我可以运行测试:

py.test --type=integration
py.test --type=unittest

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

2 个答案:

答案 0 :(得分:8)

你可以mark test functions

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

pytest.ini文件中的这些自定义标记必须为registered

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

py.test -v -m unittest

另一种选择是将测试分成unittestintegration目录。然后,您可以使用以下命令在特定目录中运行测试:

py.test -v unittest

py.test -v integration

答案 1 :(得分:0)

另一种方法(无需任何配置或代码)

pytest -o "python_functions=*_integration_test"

您也可以在模块/类级别进行此操作,例如

python_files = integration_test_*.py
python_classes = IntegrationTest

参考: https://docs.pytest.org/en/latest/example/pythoncollection.html#changing-naming-conventions