pybuilder and pytest: cannot import source code when running tests

2024/10/12 4:31:12

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:

@initdef 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
Answer

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_pluginuse_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://en.xdnf.cn/q/118238.html

Related Q&A

HTTPS proxy server python

I have a problem with my ssl server (in Python). I set the SSL proxy connection in my browser, and try to connect to my ssl server.This is the server:import BaseHTTPServer, SimpleHTTPServer import sslh…

Python 2.7.6 + unicode_literals - UnicodeDecodeError: ascii codec cant decode byte

Im trying to print the following unicode string but Im receiving a UnicodeDecodeError: ascii codec cant decode byte error. Can you please help form this query so it can print the unicode string properl…

Retrieving data from Quandl with Python

How can I get the latest prices from a Quandl dataset with the Python API (https://www.quandl.com/help/python)? On https://www.quandl.com/help/api, it says "You can use rows=n to get only the fir…

Django: Using same object how to show 2 different results in django template?

Using the same object how to SHOW 2 different results using django template ?In one page there are two divs, it should show different information using the same object.INPUTobject data has follows[{&q…

Override attribute access precedence having a data descriptor

I have a bunch of instances of a MongoEngine model. And the profiler shows that a lot of time is spent in __get__ method of MongoEngine model fields:ncalls tottime percall cumtime percall filename:…

Understanding pythons reverse slice ( [::-1] )

I always thought that omitting arguments in the python slice operation would result into:start = 0 end = len(lst) step = 1That holds true if the step is positive, but as soon as the step is negative, l…

How to print list elements (which are also lists) in separated lines in Python

Ive checked the post and answers on the SO post Printing list elements on separated lines in Python, while I think my problem is a different one.What I want is to transform:lsts = [[1], [1, 1], [1, 2, …

Python3 threading, trying to ping multiple IPs/test port simultaineously

Full (non-working) code belowFull (working, w/o threading) code here: http://pastebin.com/KUYzNtT2Ive written a small script that does the following:Pull network information from a database Ping each I…

How to print a list of numbers without square brackets?

Im generating a list of random digits, but Im struggling to figure out how to output the digits in a single row without the square brackets?import random def generateAnswer(answerList):listofDigits =…

How to save plotly offline by running my script

I am using below code in my jupyter notebook.import pandas as pd import numpy as np %matplotlib inlinefrom plotly import __version__ from plotly.offline import download_plotlyjs, init_notebook_mode, pl…