运行 python 中的单元测试\集成测试
running unittests \ integration tests in python
我有一个包含多个应用程序的 Django 项目。每个应用程序都有一组单元测试。我正在使用 pytest 作为我的测试 运行ner。我们已经到了要开始编写集成测试的地步。我想知道是否有任何方法可以保持命名约定,从而自动发现 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 命令并 "manually" 构建一个测试套件并 运行 它与 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
这些自定义标记必须 registered 在您的 pytest.ini 文件中。
然后使用-m
标志来运行标记的测试
py.test -v -m unittest
另一种选择是将测试分成 unittest
和 integration
目录。然后你可以 运行 在特定目录中测试:
py.test -v unittest
或
py.test -v integration
另一种方法(无需任何配置或代码)
pytest -o "python_functions=*_integration_test"
您也可以在 module/class 级别执行此操作,例如
python_files = integration_test_*.py
python_classes = IntegrationTest
参考:
https://docs.pytest.org/en/latest/example/pythoncollection.html#changing-naming-conventions
我有一个包含多个应用程序的 Django 项目。每个应用程序都有一组单元测试。我正在使用 pytest 作为我的测试 运行ner。我们已经到了要开始编写集成测试的地步。我想知道是否有任何方法可以保持命名约定,从而自动发现 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 命令并 "manually" 构建一个测试套件并 运行 它与 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
这些自定义标记必须 registered 在您的 pytest.ini 文件中。
然后使用-m
标志来运行标记的测试
py.test -v -m unittest
另一种选择是将测试分成 unittest
和 integration
目录。然后你可以 运行 在特定目录中测试:
py.test -v unittest
或
py.test -v integration
另一种方法(无需任何配置或代码)
pytest -o "python_functions=*_integration_test"
您也可以在 module/class 级别执行此操作,例如
python_files = integration_test_*.py
python_classes = IntegrationTest
参考: https://docs.pytest.org/en/latest/example/pythoncollection.html#changing-naming-conventions