py.test not using mocks

已回答

I am evaluating PyCharm professional. I have a remote interpreter and deployment running on an AWS EC2 instance running Amazon Linux. I can run my code, and I can even run tests in py.test. However, if those tests use any mocks, the mocks are ignored. Here's a minimum working example.

>> mwe.py <<

def return_one():
return 1

>> test_mwe.py <<

import mwe

def test_return_one():
assert 1 == mwe.return_one()

def test_mock_return_one(mocker):
patched = mocker.patch("mwe.return_one")
patched.return_value = 2
assert 2 == mwe.return_one()

 When run at the command line, all tests pass. When run in PyCharm, "test_return_one" passes and "test_mock_return_one" fails. The interpreter is set up as the remote interpreter and the deployment is set as the remote deployment. The default test runner is py.test. I am running the tests by right-clicking on "test_mwe.py" and choosing "run py.test in test_mwe.... (Ctrl+Shift+F10)". 

0
Avatar
Permanently deleted user

Full (sanitized) output from PyCharm

Testing started at 11:33 AM ...
ssh://xxxx@x.x.x.x:22/usr/bin/python -u /home/xxxx/.pycharm_helpers/pycharm/_jb_pytest_runner.py --path /home/xxxx/pycharm_deployment/src/test_mwe.py
Launching py.test with arguments /home/xxxxx/pycharm_deployment/src/test_mwe.py in /home/xxxxx/pycharm_deployment/src

============================= test session starts ==============================
platform linux2 -- Python 2.7.12, pytest-3.3.1, py-1.5.2, pluggy-0.6.0
rootdir: /home/xxxxx/pycharm_deployment/990_structured/src, inifile:
plugins: mock-1.6.3, hypothesis-3.42.2
collected 2 items
test_mwe.py .F [100%]
test_mwe.py:5 (test_mock_return_one)
Expected :2
Actual :1
<Click to see difference>

mocker = <pytest_mock.MockFixture object at 0x7f800c4b1bd0>

def test_mock_return_one(mocker):
patched = mocker.patch("mwe.return_one")
patched.return_value = 2
> assert 2 == mwe.return_one()
E assert 2 == 1
E + where 1 = <function return_one at 0x7f800c46e0c8>()
E + where <function return_one at 0x7f800c46e0c8> = mwe.return_one

test_mwe.py:9: AssertionError


=================================== FAILURES ===================================
_____________________________ test_mock_return_one _____________________________

mocker = <pytest_mock.MockFixture object at 0x7f800c4b1bd0>

def test_mock_return_one(mocker):
patched = mocker.patch("mwe.return_one")
patched.return_value = 2
> assert 2 == mwe.return_one()
E assert 2 == 1
E + where 1 = <function return_one at 0x7f800c46e0c8>()
E + where <function return_one at 0x7f800c46e0c8> = mwe.return_one

test_mwe.py:9: AssertionError
====================== 1 failed, 1 passed in 0.03 seconds ======================
Process finished with exit code 0

0

Hi Mitdabeisein5! Looks like Run working directory differs from the one selected during the terminal manual call (which is pwd) and this cause mocker.patch("mwe.return_one") to patch non-existed function, it should be src.mwe.return_one probably. But there's not enough info for me to reproduce it locally. Could you please specify what directory is the project root? And what working directory is selected inside Test Run Configuration?

0
Avatar
Permanently deleted user

Hi Pavel,

I appreciate your help! I'm new to PyCharm (and py.test, which makes diagnosis harder), and I'm pretty sure that you're right that this is a relative directory problem. In general, if the code is located in ~myname/my_proj/src/my_code.py and the test is located in ~myname/my_proj/src/test/test_my_code.py, and if the project root is ~myname/my_proj/, what settings should I change in PyCharm to make the tests run correctly? From the command line, I can do:

cd ~myname/my_proj/src/

pytest

At that point, py.test seems to automagically see the source code as if it were in the same directory, even though it's not. For PyCharm, that doesn't seem to happen, so I'm sure I need to configure something. There's also the remote server mapping, which adds another layer of confusion.

For this particular case: I assume that by "project root" you mean the directory in which the project is located, which is D:\dmz\PyCharm\my_project, which maps to /home/myname/pycharm_development/my_project on the EC2 instance. I had to poke around to find the test run configuration. If I'm looking at the right thing, I have D:\dmz\PyCharm\my_project\src as the working directory in "Run/Debug Configurations" for the invocation of the test that didn't work.

David

0

请先登录再写评论。