8

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

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.