如何正确配置pybuilder和pytest的路径

时间:2017-06-09 10:38:43

标签: python pytest pybuilder

目的

我想让PyBuilder与pytest一起使用来测试我的python脚本。我正在与PyCharm合作。

我试图通过pybuilder and pytest: cannot import source code when running tests,但这不起作用。

设置

我的项目结构是

<root>
|- src
     |-main
         |-python
            |-data_merger
                |- gRPC
     |-unittest
         |-python

gRPC包含我的python文件,unittest/python包含我的包含测试的python文件。

所有文件夹都包含空__init__.py个文件。

我正在使用Anaconda并安装到这个pybuilder,pytest和pytest-pythonpath中,这要归功于上述帖子。

conda install pytest
pip install pybuilder
pip install pytest-pythonpath

我使用内容

创建了一个文件<root>/pytest.ini
[pytest]
python_paths = src/main/python

以及内容为

的文件<root>/build.py
from pybuilder.core import init, use_plugin

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

default_task = "publish"

@init
def initialize(project):
    project.build_depends_on('mockito')
    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)

@init
def set_properties(project):
    project.set_property("dir_source_main_python", r"src/main/python/")
    # project.set_property("dir_source_integrationtest_python", r"src/tests/integrationtest")
    project.set_property("dir_source_unittest_python", r"src/unittest/python")
    project.set_property("unittest_module_glob", "*_tests.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)

错误

当我运行pybpyb run_unit_tests各自的命令时,我收到以下错误:

PyBuilder version 0.10.36
Build started at 2017-06-09 12:33:28
------------------------------------------------------------
[INFO]  Building myproject version 1.0-SNAPSHOT
[INFO]  Executing build in ~/myproject
[INFO]  Going to execute task publish
[INFO]  Executing unittest Python modules in ~/myproject/src/unittest/python
[INFO]  Executed 2 unittests
[ERROR] Test has error: unittest.loader._FailedTest.children_statistics_tests
[ERROR] Test has error: unittest.loader._FailedTest.clusterEvents_tests
##teamcity[testStarted name='children_statistics_tests (unittest.loader._FailedTest)']
##teamcity[testFailed name='children_statistics_tests (unittest.loader._FailedTest)' message='See details' details='<class ImportError>: Failed to import test module: children_statistics_tests
Traceback (most recent call last):
  File "~/anaconda2/envs/myenv/lib/python3.6/unittest/loader.py", line 153, in loadTestsFromName
    module = __import__(module_name)
  File "~/myproject/src/unittest/python/children_statistics_tests.py", line 5, in <module>
    import src.main.python.gRPC.event_clustering_module as ec
ModuleNotFoundError: No module named src
']
##teamcity[testFinished name='children_statistics_tests (unittest.loader._FailedTest)']
##teamcity[testStarted name='clusterEvents_tests (unittest.loader._FailedTest)']
##teamcity[testFailed name='clusterEvents_tests (unittest.loader._FailedTest)' message='See details' details='<class ImportError>: Failed to import test module: clusterEvents_tests
Traceback (most recent call last):
  File "~/anaconda2/envs/myenv/lib/python3.6/unittest/loader.py", line 153, in loadTestsFromName
    module = __import__(module_name)
  File "~/myproject/src/unittest/python/clusterEvents_tests.py", line 3, in <module>
    import src.main.python.gRPC.event_clustering_module as ec
ModuleNotFoundError: No module named src
']
##teamcity[testFinished name='clusterEvents_tests (unittest.loader._FailedTest)']
------------------------------------------------------------
BUILD FAILED - There were 2 test error(s) and 0 failure(s)
------------------------------------------------------------
Build finished at 2017-06-09 12:33:28
Build took 0 seconds (832 ms)

我改变了

中提到的行
import src.main.python.gRPC.event_clustering_module as ec

import gRPC.event_clustering_module as ec

但结果是

$ pytest
========================================================================================== test session starts ===========================================================================================
platform linux -- Python 3.6.1, pytest-3.1.1, py-1.4.33, pluggy-0.4.0
rootdir: ~/myproject, inifile: pytest.ini
plugins: pythonpath-0.7.1
collected 0 items 

====================================================================================== no tests ran in 0.02 seconds ======================================================================================

然后我将pytest.ini更改为

[pytest]
python_paths = src/unittest/python

但这并没有改变任何事情。运行pybpyb run_unit_tests会导致

[INFO]  Executing unittest Python modules in ~/myproject/src/unittest/python
[WARN]  No unittests executed.
[INFO]  All unittests passed.

问题

显然,有关路径的某些设置不起作用。我错过了什么,我需要做什么?

0 个答案:

没有答案