pybuilder and pytest: cannot import source code when running tests

北战南征 提交于 2019-12-25 04:38:32

问题


so i have a project:

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

data_merger is marked as root (I am using Pycharm). This is part of my build file:

@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)

when I build my project i get the following error that it cannot import my source code:

 ←[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:


This is a known issue: https://github.com/pybuilder/pybuilder/issues/13.

I ended up using pytest-pythonpath. Here is the relevant part of my 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)

And here is my pytest.ini :

[pytest]
python_paths = src/main/python

And now it works perfectly:

$ pyb run_unit_tests


来源:https://stackoverflow.com/questions/30510715/pybuilder-and-pytest-cannot-import-source-code-when-running-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!