In the Indirect parametrization example I want to type hint request.param indicating a specific type, a str for example. The problem is since the argument to fixt must be the request fixture there seems to be no way to indicate what type the parameters passed through the “optional param attribute” should be (quoting the documentation). What are the alternatives? Perhaps
Tag: pytest
Pytest: Mock multiple calls of same method with different side_effect
I have a unit test like so below: As part of the foo.run() code run, get_request is called multiple times. I want to have a different side_effect function for each call of get_request method, in this case it is side_effect_func1, side_effect_func2, side_effect_func3. But what I’m noticing is that only m1 mock object is active, i.e only side_effect_func1 is invoked but
__init__.py needed for imports to work in Pytest. But using Python 3.8
I have read that after Python 3.3, __init__.py is not required anymore, So I am not understanding why I need to add it (__init__.py) for my pytests to work. Directory structure: In /foo/bar.py I have the following code In test_bar.py I have the following code I run the tests from /root like this: If I place a __init__.py file in
Python add pytest –black to test suite
I use pytest for testing my Python project. What I want to do is to add to my test suite a function to check whether my code is formatted in “Black” or not. When I press the command “pytest –black” my whole project is tested as I want to. How can I add this function in my tests suite and
How to test a Faust agent that sends data to a sink?
I am trying to write unit tests using pytest for my Faust application. I have referred to the documentation here but it does not mention what to do when my Faust agent is sending data to a sink. Without a sink, my tests are working fine but when I use a sink, I get this error: I have tried various
python3.7 subprocess failed to delete files for me
I have a python script using ‘subprocess’ running linux command to confirm my task is doing the right thing, and it worked well. But i found that at the same time it will generate some log files when running my task. So i added a clean up function to rm log files for me at the beginning. My script is:
Pytest + mock: patch does not work without with clause
I’m testing complex logic that require joining of central fact table with 10-20 smaller dimensional tables. I want to mock that 10-20 smaller tables. How to patch methods return values in a for loop? See code below. tables.py: test_logic.py P.S. alternatively I can try to mock the BaseClass.load, but then I don’t know how to return the different data set
running a package pytest with poetry
I am new to poetry and want to get it set-up with pytest. I have a package mylib in the following set-up in test_functions I have However, when I run it complains about mylib not being included. I can run but that clutters my python environment with mylib. I want to use that environment as well for other packages. My
Coverage for one-liner if statement
Static code analyzers for Python do not generate branches in the following case (one-liner if). Is this by design? Even coverage gives 100% coverage even if only one case is tested. Can anyone please shed some light on this? Answer The first comment says “does not create a control structure.” I don’t know what that means. Whether it’s one line
Changing monkeypatch setattr multiple times
I am trying to test code that depends on a third party and would like to use monkeypatch to replicate what I expect a request will return. Here is a minimal example of the code that I have. For my tests, I have something like the following: How can I monkeypatch each of the calls to requests.get using monkeypatch.setattr? The