-
-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python3 + pytest + pytest-mock: Mocks leaking into other test functions breaking assertions? #84
Comments
Hi, sorry for not answering earlier, I was out of town. Looking at the code I don't see anything obvious, but because I can't run the code I can't dig deeper. You think Here some suggestions to try to sort the issue:
Other than that I don't know how to help further, but please keep me posted if you discover anything. |
Hi @nicoddemus ! Your timing is amazing, thank you for responding. So, I haven't fully fixed things yet( though part of it might be from some weird crap I was trying ), but you're spot on about differences between
I set |
@nicoddemus okay! Repoduced it in a "simpler" separate repo. Going to copy and paste the readme I made as well here: https://github.com/bossjones/reproduce_pytest_mock_issue_84 If I can help in anyway, please let me know. Maybe this isn't even Readme:reproduce_pytest_mock_issue_84reproduce_pytest_mock_issue_84 DisclaimerI put this repo together pretty quickly, in between the work week in an effort to repoduce the issues I saw while working w/ Requirements
Specifically i'm using:
Setup1. Start docker container via docker-compose
2. Exec into the container using bash
3. Once in the container, run bootstrap command to install dependencies
4. Enable virtualenv and run testsA. How to repoduce error
B. Run tests w/ mocker.stopall() to fix "leak"When you set this environment variable, mocker.stopall() runs at the beginning and end of each test case.
|
Exception text seems very similar to http://programtalk.com/python-examples/qibuild.build.BuildFailed/ example 2 "test_qibuild_make.py". Perhaps there is a bug? |
Very interesting... For the sake of transparency, the comment @dimaqq is referring to is:
I definitely have |
I'm experiencing the same issue — I'm currently working around it by keeping my tests in reverse order, i.e. the ones testing full functionality first, then those that rely on mocking out certain functionality. |
@lohrmann which issue, the original leaks or the one mentioned by @dimaqq? About calling Lines 157 to 165 in 433378c
So I'm at a loss regarding that. I thought pytest-dev/pytest#2798 might be related, but doesn't seem the case because there are no failures. |
In the last case @bossjones mentioned, can't the issue be that there is a |
@fogo that is not recommended and might cause problems specially done like that link, thanks. |
@nicoddemus Seems to be the first issue to me. I'm a bit out of my depth here as I'm just a 'hobby coder' but wanted to make sure to let you know. If there is any extra information that would be helpful for you let me know. If a function (from the module I'm testing) is called in a test but was previously mocked out it will still reference the mock. The other way around works. |
That shouldn't happen, the |
Sure — I have put the following together which reproduces the problem on my system. Perhaps I am just using the functionality incorrectly though? min.py
test_min.py
|
The problem is that you are assigning the mock object directly: m.ClassTest.fun_test = mocker.MagicMock()
Use mocker.patch.object(m.ClassTest, 'fun_test') Then your example works as expected for me:
|
Ahh, I see. PEBKAC after all. Thanks for the explanation! |
TIL what PEBKAC means 😉 You're welcome! |
The sys.argv is not needed anymore because the arguments are passed from main() as a parameter. This eases testing. There are few exceptions that are not sorted yet. The help command has additional parameters 'tests' and 'examples' which requires checking of the sys.argv. There is one test commented out. It may be that the exception set by the side_effect for ImportError turns this setting global. This affects to following test. /1/ pytest-dev/pytest-mock#84 Signed-off-by: Heikki Laaksonen <laaksonen.heikki.j@gmail.com>
Hi guys! First of all, thank you for everything that you do! It is really appreciated!
My Question
Apologies in advance if this issue is blatant, but i've been wrestling with it for several days now. Hopefully someone can shed some new light.
I'm in the process of converting unit tests for my personal project from
unittest
->pytest
. Previously I was using the built-inunittest.mock
module, but now i'm trying to use thepytest-mock
plugin instead.I have a sneaking feeling that my tests are leaking mock objects into one another.
Here's why:
NOTE: All Details about my setup (python version, modules etc) listed at bottom of question.
High-level details:
When I run my tests using the following command:
py.test --pdb --showlocals -v -R : -k test_subprocess.py
Everything is fine till we get to
test_subprocess_check_command_type
. At which point I get the following error:BUT!
If I filter out all of the other tests except for the problematic one then I get:
via
py.test --pdb --showlocals -v -R : -k test_subprocess_check_command_type
I also tried manually commenting out the following 2 tests and they allowed me to successfully run all the tests again:
test_subprocess_init
test_subprocess_map_type_to_command
Can anyone see anything blatently wrong with my setup? I've read several blog posts on "where to mock", and looked at the docs themselves several times, not sure what i'm missing. https://docs.python.org/3/library/unittest.mock.html
My Setup Details
Here is everything that might be required to solve this. Let me know if I need to provide any more information!
Also ... please excuse how messy my code looks and all of the comment blocks. I'm a big note taker when i'm learning something new ... I'll make everything more pythonic and cleaner in the near future :)
My code:
My Test:
My folder structure( Note I removed a couple things since it was overly verbose ):
Other details( Extended pip freeze just in case of incompatibilities ):
The text was updated successfully, but these errors were encountered: