fixtures

Python monkeypatch.setattr() with pytest fixture at module scope

Python monkeypatch.setattr() with pytest fixture at module scope Question: First of all, the relevant portion of my project directory looks like: └── my_package ├── my_subpackage │ ├── my_module.py | └── other_module.py └── tests └── my_subpackage └── unit_test.py I am writing some tests in unit_test.py that require mocking of an external resource at the module level. …

Total answers: 2

pytest: how to use a mark to inject a fixture?

pytest: how to use a mark to inject a fixture? Question: I’m writing a pytest plugin with a fixture that has a side effect of setting up some desirable mocks. I’d like to write a simple mark that will allow the user to call this fixture setup before the test runs, without having to include …

Total answers: 2

difference between fixture and yield_fixture in pytest

difference between fixture and yield_fixture in pytest Question: I am going through pytest fixtures, and the following looks pretty similar, latest works pretty similar. Yes, the readability is better in yield_fixure, however could someone let me know what exactly is the difference. which should I use, in cases like mentioned below? @pytest.fixture() def open_browser(request): print(“Browser …

Total answers: 2

Pytest use same fixture twice in one function

Pytest use same fixture twice in one function Question: For my web server, I have a login fixture that create a user and returns the headers needed to send requests. For a certain test, I need two users. How can I use the same fixture twice in one function? from test.fixtures import login class TestGroups(object): …

Total answers: 6

Django : loaddata to update data

Django : loaddata to update data Question: I have a fixture seed_data.json on which I have my initial data. Sometimes I add new data into this fixtures and reload it, which updates my data correctly. However, I now want to remove some data from it. So I modified my seed_data.json, for instance, I had something …

Total answers: 2

Autousing pytest fixtures defined in a separate module

Autousing pytest fixtures defined in a separate module Question: I have the following file tree in my project: … tests/ __init__.py test_abc.py … I have a fixture defined in __init__.py: @pytest.fixture(autouse=True) def client(): return TestClient() I want to use this fixture in test_abc.py: from . import client def test_def(): client.get(…) # Throws an error How …

Total answers: 2

@Patch decorator is not compatible with pytest fixture

@Patch decorator is not compatible with pytest fixture Question: I have encountered something mysterious, when using patch decorator from mock package integrated with pytest fixture. I have two modules: —–test folder ——-func.py ——-test_test.py in func.py: def a(): return 1 def b(): return a() in test_test.py: import pytest from func import a,b from mock import patch,Mock …

Total answers: 7

Pass a parameter to a fixture function

How to pass a parameter to a fixture function in Pytest? Question: I am using py.test to test some DLL code wrapped in a python class MyTester. For validating purpose I need to log some test data during the tests and do more processing afterwards. As I have many test_… files I want to reuse …

Total answers: 14

How and where does py.test find fixtures

How and where does py.test find fixtures Question: Where and how does py.test look for fixtures? I have the same code in 2 files in the same folder. When I delete conftest.py, cmdopt cannot be found running test_conf.py (also in same folder. Why is sonoftest.py not searched? # content of test_sample.py def test_answer(cmdopt): if cmdopt …

Total answers: 3

python unittests with multiple setups?

python unittests with multiple setups? Question: I’m working on a module using sockets with hundreds of test cases. Which is nice. Except now I need to test all of the cases with and without socket.setdefaulttimeout( 60 )… Please don’t tell me cut and paste all the tests and set/remove a default timeout in setup/teardown. Honestly, …

Total answers: 6