I've created a project in Pycharm that I can successfully run and as well its unit tests. However, when I try to run pytest from the command line in the location where the tests folder is I get an error where the tests protest that it can't find objects in the given module (under test). I know that Pycharm runs certain commands before launching anything and exports the information through PYTHONPATH to make its tests work but how do I do the same thing without PyCharm?
The following shows my project structure:
PycharmProjects\
ProjectFolder\
- setup.py
- readme.txt
module\
- __init__.py
- module.py
tests\
- __init__.py
- test_module.py
Before Pycharm runs my code, it does the following at the time when the script starts up (I got this from Settings > Console > Python Console). I don't know how to tell what is inside WORKING_DIR_AND_PYTHON_PATHS.
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS])
However, when I run from the command line when I am at the Project Folder I get the following type of error. What gives??
D:\user\development\PycharmProjects\ProjectFolder> pytest
============================= test session starts =============================
platform win32 -- Python 2.7.11, pytest-3.1.3, py-1.4.34, pluggy-0.4.0
rootdir: D:\user\PycharmProjects\ProjectFolder, inifile:
collected 4 items
tests\test_module.py .F.F
================================== FAILURES ===================================
_______________________ TestModule.test_ONE ________________________
self = <tests.test_module.TestModule testMethod=test_ONE>
def test_ONE(self):
> someObject = module.SomeObject("someObject.log")
E AttributeError: 'module' object has no attribute 'SomeObject'
tests\test_module.py:23: AttributeError
________________________ TestModule.test_TWO _________________________
self = <tests.test_module.TestModule testMethod=test_TWO>
def test_TWO(self):
> someObject = module.SomeObject("someObject.log")
E AttributeError: 'module' object has no attribute 'SomeObject'
tests\test_module.py:8: AttributeError
===================== 2 failed, 2 passed in 0.14 seconds ======================

PYTHONPATH=D:\user\PycharmProjects\ProjectFolder? What does yourimportstatement inside oftests\test_module.pylook like formodule? In this scenario it would need to befrom module import module, andSomeObjectwould be aclassinsideD:\user\PycharmProjects\ProjectFolder\module\module.pyPYTHONPATH=D:\user\PycharmProjects\ProjectFolder2). Insidetests\test_module.py, the import saysimport module; 3). I tried thefrom module import moduleand I get the following error now in PyCharm (but the command line now works) ---from module import module, ImportError: cannot import name modulePYTHONPATH=D:\user\PycharmProjects\ProjectFolder;D:\user\PycharmProjects\ProjectFolder\moduleand your import statementimport moduleI bet it would work in both, but that's not really the correct way of doing it. Try changing your project content rootSettings | Project | Project StructuretoD:\user\PycharmProjects\ProjectFolder. If that doesn't work I'd probably need to see an image of your prj struct in PyCharm