使用单元测试中的doctest

问题描述

| 我通常在模块中同时编写单元测试和文档测试。我想在运行测试套件时自动运行我的所有doctest。我认为这是可能的,但是我在语法上遇到了困难。 我有测试套件
import unittest
class ts(unittest.TestCase):
    def test_null(self): self.assertTrue(True)
if __name__ == \'__main__\': unittest.main()
我想将模块“ 1”中的所有doctest添加到该套件中。我怎样才能做到这一点?我已经阅读了python文档,但是在这里,我离成功还很近。添加
import doctest
import module1
suite = doctest.DocTestSuite(module1)
不起作用。
unittest.main()
搜索当前文件范围并运行找到的每个测试用例,对吗?但是DocTestSuite产生了一个测试套件。我如何获得ѭ3来运行套件中的其他案例?还是我只是困惑和迷惑? 再次感谢任何人都可以提供的帮助。     

解决方法

在这段代码中,我结合了导入模块的单元测试和文档测试
import unittest


class ts(unittest.TestCase):
    def test_null(self):
        self.assertTrue(True)


class ts1(unittest.TestCase):
    def test_null(self):
        self.assertTrue(True)

testSuite = unittest.TestSuite()    
testSuite.addTests(unittest.makeSuite(ts))
testSuite.addTest(unittest.makeSuite(ts1))

import doctest
import my_module_with_doctests

testSuite.addTest(doctest.DocTestSuite(my_module_with_doctests))
unittest.TextTestRunner(verbosity = 2).run(testSuite)
    ,这个老问题的更新:自python版本2.7起,有load_tests协议,不再需要编写自定义代码。它允许您添加一个函数“ 6”,测试加载器将执行该函数以更新其当前模块的单元测试集合。 在您的代码模块中放置一个类似这样的函数,以将模块自己的doctest打包到用于ѭ7的测试套件中:
def load_tests(loader,tests,ignore):
    tests.addTests(doctest.DocTestSuite())
    return tests
或者,将这样的函数放入您的单元测试模块中,以将另一个模块(例如,
package.code_module
)中的doctests添加到已经存在的测试套件中:
def load_tests(loader,ignore):
    tests.addTests(doctest.DocTestSuite(package.code_module))
    return tests
当使用
unittest.TestLoader
方法
loadTestsFromModule()
loadTestsFromName()
discover()
时,unittest使用的测试套件包括单元测试和doctest。     ,我建议在没有任何load_test协议的情况下使用ѭ15。您可以简单地将带有常规pytests的文件或目录以及带有doctests的模块添加到该pytest调用中。   pytest --doctest-modules路径/到/ pytest / unittests路径/到/模块 它还会发现并运行所有doctest。 参见https://docs.pytest.org/en/latest/doctest.html     ,此代码将自动为包中的所有模块运行doctest,而无需为每个模块手动添加测试套件。可以与Tox一起使用。
import doctest
import glob
import os
import sys
if sys.version_info < (2,7,):
    import unittest2 as unittest
else:
    import unittest

import mypackage as source_package

def load_module_by_path(path):
    \"\"\"Load a python module from its path.

    Parameters
    ----------
    path : str
        Path to the module source file.

    Returns
    -------
    mod : module
        Loaded module.
    \"\"\"
    import imp

    module_file_basename = os.path.basename(path)
    module_name,ext = os.path.splitext(module_file_basename)
    mod = imp.load_source(module_name,path)
    return mod

def file_contains_doctests(path):
    \"\"\"Scan a python source file to determine if it contains any doctest examples.

    Parameters
    ----------
    path : str
        Path to the module source file.

    Returns
    -------
    flag : bool
        True if the module source code contains doctest examples.
    \"\"\"
    with open(path) as f:
        for line in f:
            if \">>>\" in line:
                return True
    return False

def load_tests(loader,pattern):
    \"\"\"Run doctests for all modules\"\"\"
    source_dir = os.path.dirname(source_package.__path__[0])
    python_source_glob = os.path.join(source_dir,source_package.__name__,\"*.py\")
    python_source_files = glob.glob(python_source_glob)
    for python_source_file in python_source_files:
        if not file_contains_doctests(python_source_file):
            continue
        module = load_module_by_path(python_source_file)
        tests.addTests(doctest.DocTestSuite(module))
    return tests
    ,zope.testing模块提供了这样的功能。 看到 http://www.veit-schiele.de/dienstleistungen/schulungen/testen/doctests 举些例子。