读取python代码以生成单元测试存根
PyTestStub的Python项目详细描述
pyteststub
pyteststub读取python代码以生成单元测试存根。给定一个模块名, 它遍历模块中的每个文件。如果遇到没有 对应的测试文件,它为每个函数生成一个带有测试存根的测试文件 以及文件中的类方法。
除了减少在样板上花费的时间之外,这种方法还确保了 创建新测试时的覆盖率,这样开发人员可以专注于实际的测试。 在生成之后,返工仅限于移除不需要的存根和复制 需要多次测试(复制+粘贴)的测试。生成的骨架是 足够完整,可以将测试实现委托给另一个开发人员。
安装
要安装,只需使用pip
:
> python3 -m pip install PyTestStub
脚本
GenerateUnitTests.py
生成实际的单元测试,并将头文件等选项作为 许可证:
> python3 -m PyTestStub.GenerateUnitTests -h
usage: GenerateUnitTests.py [-h] [-F FOOTER] [-H HEADER] [-f] [-m TEST_MODULE]
[-p TEST_PREFIX] [-t TAB_WIDTH]
module
Python Unit Test Stub Generator
positional arguments:
module The path of the module to test.
optional arguments:
-h, --help show this help message and exit
-F FOOTER, --footer FOOTER
File to use as a footer.
-H HEADER, --header HEADER
File to use as a header.
-f, --force Force files to be generated, even if they already
exist.
-m TEST_MODULE, --test-module TEST_MODULE
The path of the test module to generate.
-p TEST_PREFIX, --test-prefix TEST_PREFIX
The prefix for test files.
-t TAB_WIDTH, --tab-width TAB_WIDTH
The width of a tab in spaces (default actual tabs).
输出简单且可读:
> python3 -m PyTestStub.GenerateUnitTests PyTestStub
No classes or functions in PyTestStub/__init__.py
Writing test to test/test_Generator.py
No classes or functions in PyTestStub/Templates.py
输出文件的所有内容都有存根,但是如果安装程序 不需要方法:
import unittest
class GeneratorTest(unittest.TestCase):
"""
Tests for functions in the Generator module.
"""
@classmethod
def setUpClass(cls):
pass #TODO
@classmethod
def tearDownClass(cls):
pass #TODO
def setUp(self):
pass #TODO
def tearDown(self):
pass #TODO
def test_generateUnitTest(self):
raise NotImplementedError() #TODO: test generateUnitTest