I am using pycharm at one of my university projects and I wanted to integrated it with unittest
module, but I have a problem with structuring my project
Part of this project involves generating abstract syntax trees, so I created AST
directory and put __init__.py
there, then I created expression
module. I wanted to put my tests in test/
subdirectory, so it would look like this:
AST/
__init__.py
expression.py
test/
some_test.py
utils.py
now I have also module in my AST
called symbol_table
and module called utils
, example test class looks like
import unittest
from ...AST import expression
from ...AST import utils
class ConstantExpressionTest(unittest.TestCase):
def testConstantExpressionCheck(self):
constantExpression = expression.ConstantExpression(17, 5, utils.TYPES.INT)
self.assertTrue(constantExpression.check())
when I right click on this file and select Run Unittest in ...
I am getting errors:
/usr/bin/python2.7 /home/xubuntu/Downloads/pycharm-2.7.2/helpers/pycharm/utrunner.py /home/xubuntu/Przedmioty/VI/kompilatory/tk-projekt/src/AST/test/test_constant_expression.py true
Testing started at 12:06 PM ...
Traceback (most recent call last):
File "/home/xubuntu/Downloads/pycharm-2.7.2/helpers/pycharm/utrunner.py", line 110, in <module>
modules = [loadSource(a[0])]
File "/home/xubuntu/Downloads/pycharm-2.7.2/helpers/pycharm/utrunner.py", line 34, in loadSource
module = imp.load_source(moduleName, fileName)
File "/home/xubuntu/Przedmioty/VI/kompilatory/tk-projekt/src/AST/test/test_constant_expression.py", line 2, in <module>
from ...AST import utils
ValueError: Attempted relative import in non-package
Process finished with exit code 1
I have read about this problem and if I understand this right, this file is treated as it would be in top-level package so I can't use any relative imports.
But if that is the case, how can I run unit-tests from pycharm and also keep my current project strcture?
If I am not mistaken, putting tests in sub-package is pretty popular (http://as.ynchrono.us/2007/12/filesystem-structure-of-python-project_21.html) so there must be some kind of solution
If no specific test runner is installed, PyCharm uses unittest. To explicitly set the required test runner in the project settings, press Ctrl+Alt+S to open the IDE settings and select Tools | Python Integrated Tools, and then select the target test runner from the Default test runner list.
Do one of the following: Right-click the selected file or folder, and choose Run <feature file name> on the context menu of the selection. Create run/debug configuration for one of the BDD frameworks, and specify the desired file of folder there.
PyCharm auto-detects a test runner that is installed on your Python interpreter and uses it to run tests.
Well, that is a bit silly, I found out that pycharm adds the root of the project to path so I can just use normal imports from the root of my project.
So for example I can write
from AST import expression
in my some_test
file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With