python - 单元测试 : How can i import test classes dynamically and run?

标签 python unit-testing testing automated-tests

我正在做一个简单的脚本来运行和测试我的代码。我如何动态导入并运行我的测试类?

最佳答案

这是我找到的用于导入和动态运行我的测试类的解决方案。

import glob
import os
import imp
import unittest

def execute_all_tests(tests_folder):
    test_file_strings = glob.glob(os.path.join(tests_folder, 'test_*.py'))
    suites = []
    for test in test_file_strings:
        mod_name, file_ext = os.path.splitext(os.path.split(test)[-1])
        py_mod = imp.load_source(mod_name, test)
        suites.append(unittest.defaultTestLoader.loadTestsFromModule(py_mod))
    text_runner = unittest.TextTestRunner().run(unittest.TestSuite(suites))

关于python - 单元测试 : How can i import test classes dynamically and run?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31682362/

相关文章:

Tornado 中页面上的 Python web 开发动态字段 id

python - 将 (n_samples, n_features) ndarray 转换为 (n_samples, 1) 向量数组,用作 sklearn SVM 的训练标签

c# - 我应该在单元测试中使用 AutoMapper 吗?

unit-testing - Clojure 中 with-test 的共享定义

testing - 如何使用 Selenium 强制执行 onClick 事件?

ruby-on-rails - 如何测试类主体方法调用?

python - Tensorflow梯度为0,权重不更新

unit-testing - 如何在Visual Studio 2017中禁用测试发现?

scalacheck 案例类随机数据生成器

python - 'pydoc -w EXPRESSIONS' 不起作用,但 'pydoc EXPRESSIONS' 起作用。为什么? (所有帮助主题均大写)