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 check the same thing when I run the “python setup.py pytest” command? Answer pytest.ini has an addopts key which does literally what it says on the tin: it adds options to the pytest command line you used. So at the
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: but it turns out it did not remove log files for me after i added first test, it still have more and more logs file in my build dir. I run it using: My question is: 1. Why did not
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 for different table (class). Answer Under the assumption that do_join shall be called outside the loop, with all tables patched, you could write a fixture that uses contextlib.ExitStack to setup all mocks: This means that all mocks are still active
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 or many lines, the compiled byte code is nearly the same: The reason coverage.py can’t tell you about the one-line case is because of the trace function that Python uses to tell coverage.py what is going on. The trace function
I’m trying to install pytest using pip but running into this error: Pretty sure pytest is compatible with python 2. Any reason why I am not able to install it on my machine? As you can see in the error, I am running python 2.7.10 and do not have issue installing other packages. Answer Quoting the changelog: The 4.6.X series will be the last series to support Python 2 and Python 3.4. Therefore, use to install the latest pytest version that supports Python 2.7.
Suppose I have a test expressed as a simple script with assert-statements (see background for why), e.g How would I include this script in my pytest test suite — in a nice way? I have tried two working but less-than-nice approaches: One approach is to name the script like a test, but this makes the whole pytest discovery fail when the test fails. My current approach is to import the script from within a test function: This works, but the scripts are not reported individually and test failures have a long and winding stack trace: Background The reason I have
Is it possible to disable an inspection for the whole file in PyCharm? The reason this is needed is when dealing with py.test. It uses fixtures which appear to shadow function parameters, and at the same time cause unresolved references. e.g.: There is also other warnings from py.test, such as using pytest.raises() causes a “Can not find reference ‘raises’” in pytest.py. Maybe there’s another way to fix these problems? Maybe I’m using py.test incorrectly? Answer Is it possible to disable an inspection for the whole file in PyCharm? Yes. This answer is for this question only (and not about “Maybe