pytest

How can I structure my folders to test my code:

Deadly 提交于 2021-01-29 07:38:37
问题 I'm working on a project and would like to write some unit tests for my code. Here is a minimal example of my project structure myproject └── scripts ├── tests │ └── test_func.py └── tools ├── __init__.py └── func.py func.py contains def f(x): return 0 and test_func.py contains import pytest from ..tools import f def test_f(): assert f(1)==0 When I try to run my tests with pytest pytest scripts/tests/test_func.py , I get the following error ImportError: attempted relative import with no known

How can I structure my folders to test my code:

你离开我真会死。 提交于 2021-01-29 07:37:18
问题 I'm working on a project and would like to write some unit tests for my code. Here is a minimal example of my project structure myproject └── scripts ├── tests │ └── test_func.py └── tools ├── __init__.py └── func.py func.py contains def f(x): return 0 and test_func.py contains import pytest from ..tools import f def test_f(): assert f(1)==0 When I try to run my tests with pytest pytest scripts/tests/test_func.py , I get the following error ImportError: attempted relative import with no known

How to fake Popen in test?

落花浮王杯 提交于 2021-01-29 05:51:03
问题 I've successfully Faked other module with my own Fake implementation or using monkeypatch . But in this case using both fake implementation or monkeypatch failed for subprocess.Popen : Using monkeypatch , failed. The result still the real opened windows title, not "foo". class TestController: def test_get_all_windows(self, ctrl_fixture, monkeypatch): def fake_communicate(a): return "foo" monkeypatch.setattr(subprocess.Popen, 'communicate', fake_communicate) output = ctrl_fixture.get_all

How to fake Popen in test?

痞子三分冷 提交于 2021-01-29 05:48:11
问题 I've successfully Faked other module with my own Fake implementation or using monkeypatch . But in this case using both fake implementation or monkeypatch failed for subprocess.Popen : Using monkeypatch , failed. The result still the real opened windows title, not "foo". class TestController: def test_get_all_windows(self, ctrl_fixture, monkeypatch): def fake_communicate(a): return "foo" monkeypatch.setattr(subprocess.Popen, 'communicate', fake_communicate) output = ctrl_fixture.get_all

PyTest : dynamically generating test name during runtime

坚强是说给别人听的谎言 提交于 2021-01-28 19:08:15
问题 I want to name the test dynamically during run-time when i run them with the @pytest.mark.parametrize("value",values_list) fixture. for example: values_list=['apple','tomatoes','potatoes'] @pytest.mark.parametrize("value",values_list) def test_xxx(self,value): assert value==value the final outcome i want to see is 3 tests with the following names: test_apple test_tomatoes test_potatoes i gave tried looking in to pytest documentation but i haven found anything that might shed light on this

pytest fixture finalization in the presence of errors

坚强是说给别人听的谎言 提交于 2021-01-28 18:25:45
问题 I have a test bench setup where I make heavy use of fixtures to provide resources and "items to test" to the tests I have defined. This is great, it allows my test functions to contain as little code as possible. However I find myself fighting with pytest quite a lot in the area of finalization. In pytest, fixture finalizers are conveniently described by making a generator of the form: @pytest.fixture def a_fixture(): #some setup code resource = setup_resource() yield resource #finalizer code

Exception handling and testing with pytest and hypothesis

☆樱花仙子☆ 提交于 2021-01-28 06:46:08
问题 I'm writing tests for a statistical analysis with hypothesis. Hypothesis led me to a ZeroDivisionError in my code when it is passed very sparse data. So I adapted my code to handle the exception; in my case, that means log the reason and reraise the exception. try: val = calc(data) except ZeroDivisionError: logger.error(f"check data: {data}, too sparse") raise I need to pass the exception up through the call stack because the top-level caller needs to know there was an exception so that it

standalone py.test (ignore __init__.py files)

五迷三道 提交于 2021-01-28 05:00:59
问题 I have a simple python module (let's call it M1) which is standalone (the only import is to collections ), inside a package containing ugliness. Part of the ugliness is in the package's __init__.py file. But M1 is nice and clean, and contains some test functions for use with py.test. Now, I would like to test M1 and ignore all the ugliness. But py.test wants to run __init__.py -- is there any way I can prevent this? I really can't fix the __init__.py file at this time, and I want to keep my

Pytest fixture with scope “class” doesn't works with “setup_class” method

China☆狼群 提交于 2021-01-28 01:52:56
问题 I'm currently using pytest_addoption to run my API tests, so the tests should run against the environment the user uses on the command line. In my test file, I'm trying to instantiate the UsersSupport class just once, passing the env argument. My code: conftest.py import pytest # Environments QA1 = 'https://qa1.company.com' LOCALHOST = 'https://localhost' def pytest_addoption(parser): parser.addoption( '--env', action='store', default='qa1' ) @pytest.fixture(scope='class') def env(request):

How to use pytest fixtures in a decorator without having it as argument on the decorated function

ぃ、小莉子 提交于 2021-01-27 07:08:42
问题 I was trying to use a fixture in a decorator which is intended to decorate test functions. The intention is to provide registered test data to the test. There are two options: Automatic import Manual import The manual import is trivial. I just need to register the test data globally and can then access it in the test based on its name, the automatic import is trickier, because it should use a pytest fixture. How shall it look in the end: @RegisterTestData("some identifier") def test_automatic