pycharm 和单元测试 - 构建项目 [英] pycharm and unittesting - structuring project
问题描述
我在我的一个大学项目中使用 pycharm,我想将它与 unittest
模块集成,但我在构建我的项目时遇到问题
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
这个项目的一部分涉及生成抽象语法树,所以我创建了AST
目录并将__init__.py
放在那里,然后我创建了expression
模块.我想把我的测试放在 test/
子目录中,所以它看起来像这样:
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
现在我的 AST
中还有名为 symbol_table
的模块和名为 utils
的模块,示例测试类看起来像
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())
当我右键单击此文件并选择 Run Unittest in ...
时出现错误:
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.
但如果是这样,我如何从 pycharm 运行单元测试并保持我当前的项目结构?
But if that is the case, how can I run unit-tests from pycharm and also keep my current project strcture?
如果我没记错的话,将测试放在子包中非常流行(http://as.ynchrono.us/2007/12/filesystem-structure-of-python-project_21.html) 所以一定有某种解决方案
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
推荐答案
好吧,这有点傻,我发现 pycharm 将项目的根添加到路径中,这样我就可以从我的根使用正常导入项目.
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.
例如我可以写
from AST import expression
in my some_test
file
from AST import expression
in my some_test
file
这篇关于pycharm 和单元测试 - 构建项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!