pybuilder和pytest:运行测试时无法导入源代码

时间:2015-05-28 15:20:43

标签: python automated-tests pytest pybuilder

所以我有一个项目:

<root>
|- src
     |-main
         |-python
            |-data_merger
                |- common
                |- constans
                |- controller
                |- resources
                |- rest
     |-tests
         |-unittest
         |-integrationtest

data_merger标记为root(我正在使用Pycharm)。 这是我的构建文件的一部分:

@init

def set_properties(project):
    project.set_property("dir_source_main_python", r"src\main\python\data_merger")
    project.set_property("dir_source_integrationtest_python", r"src\tests\integrationtest")
    project.set_property("dir_source_unittest_python", r"src\tests\unittest")
    project.set_property("unittest_module_glob", "*_test.py")
    project.set_property("unittest_test_method_prefix", "test_")
    project.set_property("run_unit_tests_command",
    "py.test %s" % project.expand_path("$dir_source_unittest_python"))
    project.set_property("run_unit_tests_propagate_stdout", True)
    project.set_property("run_unit_tests_propagate_stderr", True)
    project.set_property("teamcity_output", True)

当我构建我的项目时,我收到以下错误,它无法导入我的源代码:

 ←[1m[INFO] ←[0;0m  ERROR collecting src/tests/unittest/python/data_merger/controller/comparator_autom_params_test.py
←[1m[INFO] ←[0;0m src\tests\unittest\python\data_merger\controller\comparator_autom_params_test.py:6: in <module>
←[1m[INFO] ←[0;0m     from resources.diff_table import DiffTable
←[1m[INFO] ←[0;0m E   ImportError: No module named resources.diff_table

1 个答案:

答案 0 :(得分:1)

这是一个已知问题:https://github.com/pybuilder/pybuilder/issues/13

我最终使用pytest-pythonpath。以下是我build.py的相关部分:

from pybuilder.core import init, use_plugin

use_plugin("exec")
use_plugin("python.core")
use_plugin("python.unittest")

@init
def initialize(project):
    project.set_property("run_unit_tests_command", "py.test %s" % project.expand_path("$dir_source_unittest_python"))
    project.set_property("run_unit_tests_propagate_stdout", True)
    project.set_property("run_unit_tests_propagate_stderr", True)

这是我的pytest.ini

[pytest]
python_paths = src/main/python

现在它完美无缺:

$ pyb run_unit_tests