pytest使用构造函数在类中运行测试

问题描述 投票:0回答:1

我想为API编写一个测试套件。我的项目树如下所示:project tree

我需要有一个'客户端'文件,该文件带有用于初始化所有帮助程序函数的类(例如,使用参数,断言,配置等发送的实际请求)。以及实际的考试课。所以我有

class Client()
  __init__()
  def helper_function()
class TestBackend(Client)
  __init__()
  def test_something()

然后,我尝试使用pytest运行测试。我不断得到

PytestWarning:无法收集测试类'TestBackend',因为它具有init构造函数。

py.test skips test class if constructor is defined所述,这是预期的行为。 SO链接到此documentation但是出于对圣洁事物的热爱,我无法找到解决我遇到的问题的方法,而且我不认为这不是经常需要的情况。

python python-3.x automation pytest qa
1个回答
0
投票
“ __ init __()”,则py-test不会出于测试目的而收集这些类。但是,如果要初始化所有帮助程序方法和文件,则可以将该文件添加到

pytest.ini配置文件中的pythonpaths中,如下所示:-

[pytest] python_paths = . <path to helper file> 然后,您可以直接在测试脚本中导入该特定文件。
希望您能找到答案。
© www.soinside.com 2019 - 2024. All rights reserved.