[英]running unittests \ integration tests in python
我有一个包含多个应用程序的Django项目。 每个应用程序都有一组单元测试。 我正在使用pytest作为我的测试运行器。 我们已经到了一个点,我们想开始编写集成测试。 我想知道是否有任何方法可以保持命名约定,从而保持pytest的自动发现,但仍然能够(通过标志?)运行不同的测试类型。 想到的最直观的解决方案是测试方法或甚至TestCase
类(类似于JUnit中的类别)的某种装饰器。
就像是:
@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运行它......我宁愿不使用此选项。 还有其他解决方案可以帮助我吗?
谢谢
您可以标记测试功能 。
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文件中注册这些自定义标记。
然后使用-m
标志运行标记的测试
py.test -v -m unittest
另一种选择是将你的测试分成unittest
和integration
目录。 然后,您可以在特定目录中运行测试:
py.test -v unittest
要么
py.test -v integration
另一种方法(没有任何配置或代码)
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.