Har*_*uin 8 python testing integration-testing unit-testing pytest
根据Wikipedia和各种文章的介绍,最佳实践是将测试分为单元测试(首先运行)和集成测试(第二次运行),在这些测试中,单元测试通常非常快,应该在CI环境中的每个构建中都运行,但是集成测试运行时间更长,应该每天运行更多。
有没有办法在pytest中划分这些?大多数项目似乎没有多个测试文件夹,因此有没有一种方法可以确保我仅根据情况(CI与每日构建)运行单元和/或集成?在计算测试覆盖率时,我假设我必须同时运行两者。
在尝试将测试划分为这些类别时,我会采用正确的方法吗?在执行此操作的项目中是否有很好的例子?
pyl*_*ang 33
您还可以在结构上将单元测试和集成测试分离到特定目录中。以下是 A. Shaw 的文章 Getting Started With Testing in Python 中的示例文件结构:
通过结构化方法,您可以:
@pytest.mark.例子
在这里,我们单独在集成测试上运行各种测试运行器。参见project/上图中的示例目录。
随着unittest从标准库:
? python -m unittest discover -s tests/integration
Run Code Online (Sandbox Code Playgroud)
与nose:
? nose tests/integration
Run Code Online (Sandbox Code Playgroud)
与pytest:
? pytest tests/integration
Run Code Online (Sandbox Code Playgroud)
许多测试运行器都有一个自动测试发现机制,可以在子目录中找到测试。这提供了轻松运行所有测试的选择,例如
? cd <root_dir>
? pytest project/
Run Code Online (Sandbox Code Playgroud)
gmd*_*mds 12
是的,您可以使用pytest.mark装饰器标记测试。
例:
def unit_test_1():
# assert here
def unit_test_2():
# assert here
@pytest.mark.integtest
def integration_test():
# assert here
Run Code Online (Sandbox Code Playgroud)
现在,从命令行开始,您只能运行pytest -m "not integtest"单元测试,pytest -m integtest集成测试和普通测试pytest。
(您也可以根据需要装饰单元测试pytest.mark.unit,但我发现这有点乏味/冗长)
请参阅文档以获取更多信息。
| 归档时间: |
|
| 查看次数: |
994 次 |
| 最近记录: |