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

so i have a project:

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

←[1m[INFO] ←[0;0m from resources.diff_table import DiffTable

←[1m[INFO] ←[0;0m E ImportError: No module named resources.diff_table

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值