首页 > 解决方案 > pytest 应该运行在 test_module_A 的 test_func 中导入的 module_B

问题描述

这是我的代码

包:-一个

#test_module_A.py

def test_A1():
    print('abc')

def test_A2():
    import B.test_module_B


pkg:- B
#test_module_B.py

def test_B1():
    print('def')

我正在使用 pytest 从 pycharm 运行 test_module_A

输出

美国广播公司

但是我希望输出是

abc
def

谁能帮我....

在实际场景中,test_module_B 在包内存在的 100 个模块中是动态的。

我希望 pytest 将其控制从 A2() 重定向到 module_B 然后完全执行它并再次返回到模块 A 并执行任何剩余的功能......

标签: pythonpytest

解决方案


您正在导入B.test_module_B,但您并没有执行实际的测试。我想你想要以下内容:

def test_A2():
    import B.test_module_B
    B.test_module_B.test_B1()

推荐阅读