在pytest中为多个测试重用类

问题描述

我有一个相当枯燥的测试类:

class TestScenario:
    binary = './foo'
    def test_one(self,fixture1,fixture2):
        assert subprocess.check_call(binary,fixture1) == fixture2
    
    def test_two(self,fixture3,fixture4):
        assert subprocess.check_call(binary,fixture4) == 'OK'
    # about 10 more tests

我在 conftest.py 中也有一个超级整洁的 pytest_generate_tests,它从与 fixture1 位于同一文件夹中的目录 testdata 中填充夹具(Metafunc.module.__file__ 等)内容.

问题是我在对应目录下有几十个类似的二进制文件不同的内容(结构相同,内容文件数量不同)。

我有一个想法:将 test_foo.pytest_bar.py 等放在每个 foobar 二进制文件附近,然后在内部编写一个继承 TestScenarioclass TestFoo(TestScenario): binary='./foo' 的类{1}}。

├── foo
│   ├── foo.py
│   ├── testdata
│   │   └──...
│   └── test_foo.py
├── bar
│   ├── bar.py
│   ├── testdata
│   │   └──...
│   └── test_bar.py
│── ...many more similar subtrees
└── conftest.py

树看起来像这样:

test_foo.py

TestScenario内容

问题是:conftest.py 放在哪里以及如何导入?我不能把它放在 TestScenario 里面(因为它不是夹具),而且我不想用 test_[foo|bar|baz|foobar].py 作为模块安装文件(或管理导入路径)。

我在不同的目录中有几十个类似的 npm install [email protected] --save ng server 文件,我想尽可能避免重复代码,但我找不到共享代码方法

问题:

  • 如何从 conftest.py 导入类?
  • 或者把它放在文件树的什么位置?
  • 或者,也许有更好的方法而我正在做一些奇怪的事情?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)