pytest使用构造函数在类中运行测试 [英] pytest run tests inside a class with a constructor

查看:683
本文介绍了pytest使用构造函数在类中运行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to write a test suite for an API. my project tree looks like: project tree

我需要有一个客户端"文件,该文件带有用于初始化所有帮助程序函数的类(例如,使用params,asserts,configs等发送的实际请求).以及实际的考试课.所以我有

I need to have a 'client' file with classes where we initialize all the helper functions(e.g. the actual request that is sent with the params, asserts, configs etc.). And the actual test class. so I have

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

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

Then I try to run my tests with pytest. I keep getting

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

PytestWarning: cannot collect test class 'TestBackend' because it has a init constructor.

py.test所述,py.test会跳过测试类(如果构造函数)已定义,这是预期的行为. SO链接到此文档 但是出于对圣洁事物的热爱,我无法找到解决我遇到的问题的方法,而且我不认为这不是经常需要的情况.

As described by py.test skips test class if constructor is defined this is the expected behavior. the SO links to this documentation but for the love of all that is holy I'm unable to find the solution to the issue I'm having and I don't believe it's not a case that is frequently required.

推荐答案

是的,您是正确的,如果在类中定义了"__ init __()" ,则py-test将不会收集用于测试目的的类.但是,如果要初始化所有帮助程序方法和文件,则可以将该文件添加到 pytest.ini 配置文件中的pythonpath中,如下所示:-

Yes, you are correct, if "__init__()" is defined in the class, then the py-test would not collect the classes for the testing purpose. But, if you want to initialize all the helper methods and file, then you could add that file in the pythonpaths in pytest.ini configuration file like below:-

[pytest]
python_paths = . <path to helper file>

然后,您可以直接在测试脚本中导入该特定文件.

And from then, you could directly import that particular file in your test script.

希望您能找到答案.

这篇关于pytest使用构造函数在类中运行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆