Skip to main content

PyTestStub

PyTestStub reads your Python code to generate unit test stubs. Given a module name, it walks each file in the module. If it encounters a file without a corresponding test file, it generates one with test stubs for each function and class method in the file.

Besides reducing time spent on boilerplate, this approach ensures complete coverage when creating new tests, so developers can focus on the actual tests. After generation, rework is limited to removing unneeded stubs and duplicating those which require multiple tests (copy+paste). The resulting skeleton is sufficiently complete to delegate the test implementation to another developer.

Installation

To install, simply use pip:

> python3 -m pip install PyTestStub

Scripts

GenerateUnitTests.py

Generates the actual unit tests, with options like a header file to prepend as a license:

> python3 -m PyTestStub.GenerateUnitTests -h
usage: GenerateUnitTests.py [-h] [-F FOOTER] [-H HEADER] [-X EXCLUDE] [-f]
                            [-i] [-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.
  -X EXCLUDE, --exclude EXCLUDE
                        Add a child directory name to exclude.
  -f, --force           Force files to be generated, even if they already
                        exist.
  -i, --internal        Include internal classes and methods starting with a
                        _.
  -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).

Output is simple and human readable:

> 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

Output files have stubs for everything but are easily pruned if e.g. setup methods are not needed:

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

Metadata

Release files for PyTestStub 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for PyTestStub 0.0.4
File Size Uploaded
PyTestStub-0.0.4.tar.gz 5.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for PyTestStub 0.0.4
File Interpreter ABI Platform
PyTestStub-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 14.1 kB

Release files / PyTestStub-0.0.4.tar.gz

Download URL PyTestStub-0.0.4.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d2ceffd2a3fb93980b1d372f92d90fa631bce8e59d054bd1bee47eb31edc3122
BLAKE2b-256 checksum
How to use checksums
0e4ec9d37b5001ffa8c0a5a96bce527c7729ee61f3f8aada4cc959f50719c1b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.5.3

Release files / PyTestStub-0.0.4-py3-none-any.whl

Download URL PyTestStub-0.0.4-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f52f357d64a6b30cca7f275426b759135354b2f893662d92260138f6dd0746a1
BLAKE2b-256 checksum
How to use checksums
7bc02bd2753bff2c9567ec72ceb12b0a62207d831f25902c1618035413301ff3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.5.3

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page