You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用setup.py运行特定测试?控制台脚本配置报错排查

问题分析与解决

你遇到的TypeError: 'module' object is not callable错误,根源是console_scripts的格式错误。console_scripts要求的格式是命令名=模块路径:可调用对象,你直接指定了模块路径,没有指向模块内可执行的函数/方法,Python尝试调用整个模块,自然会报错。

下面是具体的实现方案:

步骤1:给测试模块添加可执行入口函数

以test_module1.py为例,根据你使用的测试框架,添加对应的main函数:

如果你用unittest框架

import unittest

# 你的测试用例(示例)
class TestModule1(unittest.TestCase):
    def test_sample(self):
        self.assertEqual(1 + 1, 2)

def main():
    # 运行当前模块的所有测试用例
    unittest.main()

if __name__ == "__main__":
    main()

如果你用pytest框架

import pytest

def main():
    # 运行当前文件的所有测试用例
    pytest.main([__file__])

if __name__ == "__main__":
    main()

步骤2:修正setup.py的entry_points配置

将console_scripts的路径指向模块内的main函数:

setup(
    ...
    entry_points={
        'console_scripts': [
            'test1=tests.test_module1:main',  # 明确指向可调用的main函数
            'test2=tests.test_module2:main',  # 同理添加test2命令对应test_module2
        ]
    },
    # 确保tests目录被识别为包(如果没配置的话)
    packages=['src', 'tests'],
    ...
)

步骤3:安装并测试

在项目根目录执行开发模式安装:

pip install -e .

之后就可以直接通过命令行运行对应的测试套件:

test1  # 运行test_module1的所有测试
test2  # 运行test_module2的所有测试

内容的提问来源于stack exchange,提问作者Anthony Luzquiños

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 23:45:42