相关疑难解决方法(0)

pytest:对同一接口的不同实现的可重用测试

想象一下,我已经实现了一个Bar在模块中调用的实用程序(可能是一个类)foo,并为它编写了以下测试.

test_foo.py:

from foo import Bar as Implementation
from pytest import mark

@mark.parametrize(<args>, <test data set 1>)
def test_one(<args>):
    <do something with Implementation and args>

@mark.parametrize(<args>, <test data set 2>)
def test_two(<args>):
    <do something else with Implementation and args>

<more such tests>
Run Code Online (Sandbox Code Playgroud)

现在想象一下,在未来我希望能够编写相同接口的不同实现.我希望这些实现能够重用为上述测试套件编写的测试:唯一需要改变的是

  1. 进口的 Implementation
  2. <test data set 1>,<test data set 2>等等.

所以我正在寻找一种以可重用的方式编写上述测试的方法,这将允许接口的新实现的作者能够通过将实现和测试数据注入其中来使用测试,而无需修改包含测试原始规范的文件.

在pytest中这样做的好方法是什么呢?

================================================== ==================

================================================== ==================

这是一个单元测试版本(不漂亮但是)有效.

define_tests.py:

# Single, reusable definition of tests for the interface. Authors of
# new …
Run Code Online (Sandbox Code Playgroud)

python unit-testing pytest

13
推荐指数
1
解决办法
2691
查看次数

标签 统计

pytest ×1

python ×1

unit-testing ×1