问题
I want to write a test suite for an API. my project tree looks like: project tree
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()
Then I try to run my tests with pytest. I keep getting
PytestWarning: cannot collect test class 'TestBackend' because it has a init constructor.
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.
回答1:
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.
I hope you find your answer.
来源:https://stackoverflow.com/questions/58662850/pytest-run-tests-inside-a-class-with-a-constructor