Failed to import test module when running test from editor

Answered

I can't seem to run individual tests. Before updating to 2017.1, if I was editing the source code for a test, I could press CTRL-SHIFT-F10 and run the test immediately. However, now if I do this, I get the following error:

Error
Traceback (most recent call last):
File "$HOME/anaconda2/envs/my_env/lib/python3.5/unittest/case.py", line 58, in testPartExecutor
yield
File "$HOME/anaconda2/envs/my_env/lib/python3.5/unittest/case.py", line 600, in run
testMethod()
File "$HOME/anaconda2/envs/my_env/lib/python3.5/unittest/loader.py", line 34, in testFailure
raise self._exception
ImportError: Failed to import test module: test_foo
Traceback (most recent call last):
File "$HOME/anaconda2/envs/my_env/lib/python3.5/unittest/loader.py", line 153, in loadTestsFromName
module = __import__(module_name)
ImportError: No module named 'tests.test_foo'

If I instead right click the test file from the Project View and click 'Run Unittests in test_foo.py' it works, but I liked being able to run an individual test. Any ideas?

 

If it help: I can't seem to change the default interpreter that's used when running via CTRL+SHIFT+F10 (right now it's using the my_env environment). I changed the interpreter in Settings > Build, Execution, Deployment > Console > Python Consolue. This correctly works when I right click the file name and run from there. However, a different interpreter is used when running the test from the editor with CTRL+SHIFT+F10. I can manually change it afterwards if I go to Run > Edit Configurations..., bit this is annoying to do for every test I want to run. Also, changing the interpreter doesn't fix this import problem. 

9 comments

Hello,

Thank you for the report.

Could you please contact pycharm-support@jetbrains.com and attach your project to let us reproduce the issue? If it's not possible, could you please attach a screenshot with automatically created Run configuration?

0

Hi,

I have this problem with PyCharm too, after upgrading to 2017 version. I can't do individual tests, the context menu works intermittently, sometime it shows the option, sometimes it doesn't. 

Please advise the fix?

Thanks

0

I had the same problem, introduced after upgrading to 2017.1, Running a single test yields "Exception: No module named 'Tests'". A fix that worked for me was making sure that I had an __init__.py file in my test directory.

0

The exact same problem for me could not be resolved by adding __init__.py because it had already been there. :)

Following discussion on this link I managed to resolve the issue by renaming the python file containing the tests. Obviously this file and root folder can't have same names in pycharm 2017.1.

*NOTE*: The problem did not exist in earlier (e.g. 2016.x) pycham versions!

0

In my case, the path for the test runner was changed.  This seems to be due to Run Configurations->Python Tests->Unittests losing the default config some how.  Any new tests that are attempted fail.  Once changing the default back, and clearing the "corrupt" run configs, it works again.

0

Hi Nick! I am sorry for the inconvenience. Losing default settings is a known bug, should be fixed in 2017.3.2.

0

Hi Pavel, that is good news!  Can you share the details, specifically how to reproduce this issue?  I was not able to identify what actions caused it.

0

Sure, the problem is covered by PY-25397 ticket, more specifically it's triggered when you have changed defaults for multiple run configurations (pytest and nose, nose and unittest and so on), here is my comment with detailed steps to reproduce: https://youtrack.jetbrains.com/issue/PY-25397#comment=27-2354203 By the way 2017.3.2 is already live.

0

Please sign in to leave a comment.