Development Tools¶
The modules described in this chapter help you write software. For example, the
pydoc
module takes a module and generates documentation based on the
module’s contents. The doctest
and unittest
modules contains
frameworks for writing unit tests that automatically exercise code and verify
that the expected output is produced. 2to3 can translate Python 2.x
source code into valid Python 3.x code.
The list of modules described in this chapter is:
typing
— Support for type hints- Relevant PEPs
- Type aliases
- NewType
- Callable
- Generics
- User-defined generic types
- The
Any
type - Nominal vs structural subtyping
- Module contents
- Special typing primitives
- Generic concrete collections
- Abstract Base Classes
- Protocols
- Functions and decorators
- Introspection helpers
- Constant
- Deprecation Timeline of Major Features
pydoc
— Documentation generator and online help system- Python Development Mode
- Effects of the Python Development Mode
- ResourceWarning Example
- Bad file descriptor error example
doctest
— Test interactive Python examples- Simple Usage: Checking Examples in Docstrings
- Simple Usage: Checking Examples in a Text File
- How It Works
- Basic API
- Unittest API
- Advanced API
- Debugging
- Soapbox
unittest
— Unit testing framework- Basic example
- Command-Line Interface
- Test Discovery
- Organizing test code
- Re-using old test code
- Skipping tests and expected failures
- Distinguishing test iterations using subtests
- Classes and functions
- Test cases
TestCase
TestCase.setUp()
TestCase.tearDown()
TestCase.setUpClass()
TestCase.tearDownClass()
TestCase.run()
TestCase.skipTest()
TestCase.subTest()
TestCase.debug()
TestCase.assertEqual()
TestCase.assertNotEqual()
TestCase.assertTrue()
TestCase.assertFalse()
TestCase.assertIs()
TestCase.assertIsNot()
TestCase.assertIsNone()
TestCase.assertIsNotNone()
TestCase.assertIn()
TestCase.assertNotIn()
TestCase.assertIsInstance()
TestCase.assertNotIsInstance()
TestCase.assertRaises()
TestCase.assertRaisesRegex()
TestCase.assertWarns()
TestCase.assertWarnsRegex()
TestCase.assertLogs()
TestCase.records
TestCase.output
TestCase.assertNoLogs()
TestCase.assertAlmostEqual()
TestCase.assertNotAlmostEqual()
TestCase.assertGreater()
TestCase.assertGreaterEqual()
TestCase.assertLess()
TestCase.assertLessEqual()
TestCase.assertRegex()
TestCase.assertNotRegex()
TestCase.assertCountEqual()
TestCase.addTypeEqualityFunc()
TestCase.assertMultiLineEqual()
TestCase.assertSequenceEqual()
TestCase.assertListEqual()
TestCase.assertTupleEqual()
TestCase.assertSetEqual()
TestCase.assertDictEqual()
TestCase.fail()
TestCase.failureException
TestCase.longMessage
TestCase.maxDiff
TestCase.countTestCases()
TestCase.defaultTestResult()
TestCase.id()
TestCase.shortDescription()
TestCase.addCleanup()
TestCase.enterContext()
TestCase.doCleanups()
TestCase.addClassCleanup()
TestCase.enterClassContext()
TestCase.doClassCleanups()
IsolatedAsyncioTestCase
FunctionTestCase
- Deprecated aliases
- Grouping tests
- Loading and running tests
TestLoader
TestLoader.errors
TestLoader.loadTestsFromTestCase()
TestLoader.loadTestsFromModule()
TestLoader.loadTestsFromName()
TestLoader.loadTestsFromNames()
TestLoader.getTestCaseNames()
TestLoader.discover()
TestLoader.testMethodPrefix
TestLoader.sortTestMethodsUsing
TestLoader.suiteClass
TestLoader.testNamePatterns
TestResult
TestResult.errors
TestResult.failures
TestResult.skipped
TestResult.expectedFailures
TestResult.unexpectedSuccesses
TestResult.shouldStop
TestResult.testsRun
TestResult.buffer
TestResult.failfast
TestResult.tb_locals
TestResult.wasSuccessful()
TestResult.stop()
TestResult.startTest()
TestResult.stopTest()
TestResult.startTestRun()
TestResult.stopTestRun()
TestResult.addError()
TestResult.addFailure()
TestResult.addSuccess()
TestResult.addSkip()
TestResult.addExpectedFailure()
TestResult.addUnexpectedSuccess()
TestResult.addSubTest()
TextTestResult
defaultTestLoader
TextTestRunner
main()
- load_tests Protocol
- Test cases
- Class and Module Fixtures
- Signal Handling
unittest.mock
— mock object library- Quick Guide
- The Mock Class
Mock
Mock.assert_called()
Mock.assert_called_once()
Mock.assert_called_with()
Mock.assert_called_once_with()
Mock.assert_any_call()
Mock.assert_has_calls()
Mock.assert_not_called()
Mock.reset_mock()
Mock.mock_add_spec()
Mock.attach_mock()
Mock.configure_mock()
Mock.__dir__()
Mock._get_child_mock()
Mock.called
Mock.call_count
Mock.return_value
Mock.side_effect
Mock.call_args
Mock.call_args_list
Mock.method_calls
Mock.mock_calls
Mock.__class__
NonCallableMock
PropertyMock
AsyncMock
AsyncMock.assert_awaited()
AsyncMock.assert_awaited_once()
AsyncMock.assert_awaited_with()
AsyncMock.assert_awaited_once_with()
AsyncMock.assert_any_await()
AsyncMock.assert_has_awaits()
AsyncMock.assert_not_awaited()
AsyncMock.reset_mock()
AsyncMock.await_count
AsyncMock.await_args
AsyncMock.await_args_list
- Calling
- Deleting Attributes
- Mock names and the name attribute
- Attaching Mocks as Attributes
- The patchers
- MagicMock and magic method support
- Helpers
unittest.mock
— getting started- Using Mock
- Mock Patching Methods
- Mock for Method Calls on an Object
- Mocking Classes
- Naming your mocks
- Tracking all Calls
- Setting Return Values and Attributes
- Raising exceptions with mocks
- Side effect functions and iterables
- Mocking asynchronous iterators
- Mocking asynchronous context manager
- Creating a Mock from an Existing Object
- Patch Decorators
- Further Examples
- Mocking chained calls
- Partial mocking
- Mocking a Generator Method
- Applying the same patch to every test method
- Mocking Unbound Methods
- Checking multiple calls with mock
- Coping with mutable arguments
- Nesting Patches
- Mocking a dictionary with MagicMock
- Mock subclasses and their attributes
- Mocking imports with patch.dict
- Tracking order of calls and less verbose call assertions
- More complex argument matching
- Using Mock
- 2to3 — Automated Python 2 to 3 code translation
test
— Regression tests package for Pythontest.support
— Utilities for the Python test suiteTestFailed
ResourceDenied
verbose
is_jython
is_android
unix_shell
LOOPBACK_TIMEOUT
INTERNET_TIMEOUT
SHORT_TIMEOUT
LONG_TIMEOUT
PGO
PIPE_MAX_SIZE
SOCK_MAX_SIZE
TEST_SUPPORT_DIR
TEST_HOME_DIR
TEST_DATA_DIR
MAX_Py_ssize_t
max_memuse
real_max_memuse
MISSING_C_DOCSTRINGS
HAVE_DOCSTRINGS
TEST_HTTP_URL
ALWAYS_EQ
NEVER_EQ
LARGEST
SMALLEST
is_resource_enabled()
python_is_optimized()
with_pymalloc()
requires()
sortdict()
findfile()
match_test()
set_match_tests()
run_unittest()
run_doctest()
setswitchinterval()
check_impl_detail()
set_memlimit()
record_original_stdout()
get_original_stdout()
args_from_interpreter_flags()
optim_args_from_interpreter_flags()
captured_stdin()
captured_stdout()
captured_stderr()
disable_faulthandler()
gc_collect()
disable_gc()
swap_attr()
swap_item()
flush_std_streams()
print_warning()
wait_process()
calcobjsize()
calcvobjsize()
checksizeof()
anticipate_failure()
system_must_validate_cert()
run_with_locale()
run_with_tz()
requires_freebsd_version()
requires_linux_version()
requires_mac_version()
requires_IEEE_754()
requires_zlib()
requires_gzip()
requires_bz2()
requires_lzma()
requires_resource()
requires_docstrings()
cpython_only()
impl_detail()
no_tracing()
refcount_test()
bigmemtest()
bigaddrspacetest()
check_syntax_error()
open_urlresource()
reap_children()
get_attribute()
catch_unraisable_exception()
load_package_tests()
detect_api_mismatch()
patch()
run_in_subinterp()
check_free_after_iterating()
missing_compiler_executable()
check__all__()
skip_if_broken_multiprocessing_synchronize()
check_disallow_instantiation()
adjust_int_max_str_digits()
SuppressCrashReport
SaveSignals
Matcher
BasicTestRunner
test.support.socket_helper
— Utilities for socket teststest.support.script_helper
— Utilities for the Python execution teststest.support.bytecode_helper
— Support tools for testing correct bytecode generationtest.support.threading_helper
— Utilities for threading teststest.support.os_helper
— Utilities for os testsFS_NONASCII
SAVEDCWD
TESTFN
TESTFN_NONASCII
TESTFN_UNENCODABLE
TESTFN_UNDECODABLE
TESTFN_UNICODE
EnvironmentVarGuard
FakePath
can_symlink()
can_xattr()
change_cwd()
create_empty_file()
fd_count()
fs_is_case_insensitive()
make_bad_fd()
rmdir()
rmtree()
skip_unless_symlink()
skip_unless_xattr()
temp_cwd()
temp_dir()
temp_umask()
unlink()
test.support.import_helper
— Utilities for import teststest.support.warnings_helper
— Utilities for warnings tests