Objective
I want to get PyBuilder to work with pytest to unittest my python scripts. I am working with PyCharm.
I tried to go via pybuilder and pytest: cannot import source code when running tests but this did not work.
Setup
My project structure is
<root>
|- src
|-main
|-python
|-data_merger
|- gRPC
|-unittest
|-python
Where gRPC
contains my python files and unittest/python
contains my python files which contain the tests.
All folders contain empty __init__.py
files.
I am using Anaconda and installed into this pybuilder, pytest and pytest-pythonpath thanks to the mentioned post.
conda install pytest
pip install pybuilder
pip install pytest-pythonpath
I created a file <root>/pytest.ini
with the contents
[pytest]
python_paths = src/main/python
and a file <root>/build.py
with the contents
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)
Errors
I get the following errors when I run pyb
or pyb run_unit_tests
the respective commands:
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)
I changed the mentioned lines from
import src.main.python.gRPC.event_clustering_module as ec
to
import gRPC.event_clustering_module as ec
but that resulted in
$ 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 ======================================================================================
Then I changed the pytest.ini
to
[pytest]
python_paths = src/unittest/python
but that did not change anything. Running pyb
or pyb run_unit_tests
results in
[INFO] Executing unittest Python modules in ~/myproject/src/unittest/python
[WARN] No unittests executed.
[INFO] All unittests passed.
Question
Obviously some setting regarding the paths does not work. What am I missing, what do I need to do?