开发工具¶
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.
本章中描述的模块列表是:
typing—— 对类型提示的支持- 相关的 PEP
- 类型别名
- NewType
- 标注可调用对象
- 泛型(Generic)
- 标注元组
- 类对象的类型
- 用户定义的泛型类型
Any类型- 名义子类型 vs 结构子类型
- 模块内容
- 主要特性的弃用时间线
pydoc--- 文档生成器和在线帮助系统- Python 开发模式
doctest--- 测试交互性的Python示例unittest--- 单元测试框架- 基本实例
- 命令行接口
- 探索性测试
- 组织你的测试代码
- 复用已有的测试代码
- 跳过测试与预计的失败
- 使用子测试区分测试迭代
- 类与函数
- 测试用例
TestCaseTestCase.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.recordsTestCase.outputTestCase.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.failureExceptionTestCase.longMessageTestCase.maxDiffTestCase.countTestCases()TestCase.defaultTestResult()TestCase.id()TestCase.shortDescription()TestCase.addCleanup()TestCase.enterContext()TestCase.doCleanups()TestCase.addClassCleanup()TestCase.enterClassContext()TestCase.doClassCleanups()
IsolatedAsyncioTestCaseFunctionTestCase
- 分组测试
- 加载和运行测试
TestLoaderTestLoader.errorsTestLoader.loadTestsFromTestCase()TestLoader.loadTestsFromModule()TestLoader.loadTestsFromName()TestLoader.loadTestsFromNames()TestLoader.getTestCaseNames()TestLoader.discover()TestLoader.testMethodPrefixTestLoader.sortTestMethodsUsingTestLoader.suiteClassTestLoader.testNamePatterns
TestResultTestResult.errorsTestResult.failuresTestResult.skippedTestResult.expectedFailuresTestResult.unexpectedSuccessesTestResult.collectedDurationsTestResult.shouldStopTestResult.testsRunTestResult.bufferTestResult.failfastTestResult.tb_localsTestResult.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()TestResult.addDuration()
TextTestResultdefaultTestLoaderTextTestRunnermain()- load_tests 协议
- 测试用例
- 类与模块设定
- 信号处理
unittest.mock--- 模拟对象库- 快速上手
- Mock 类
MockMock.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.calledMock.call_countMock.return_valueMock.side_effectMock.call_argsMock.call_args_listMock.method_callsMock.mock_callsMock.__class__
NonCallableMockPropertyMockAsyncMockAsyncMock.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_countAsyncMock.await_argsAsyncMock.await_args_list
ThreadingMock- 调用
- 删除属性
- Mock 的名称与 name 属性
- 附加 Mock 作为属性
- patch 装饰器
- MagicMock 与魔术方法支持
- 辅助对象
unittest.mock--- 上手指南test--- Python 回归测试包test.support--- 针对 Python 测试套件的工具TestFailedResourceDeniedverboseis_jythonis_androidunix_shellLOOPBACK_TIMEOUTINTERNET_TIMEOUTSHORT_TIMEOUTLONG_TIMEOUTPGOPIPE_MAX_SIZEPy_DEBUGSOCK_MAX_SIZETEST_SUPPORT_DIRTEST_HOME_DIRTEST_DATA_DIRMAX_Py_ssize_tmax_memusereal_max_memuseMISSING_C_DOCSTRINGSHAVE_DOCSTRINGSTEST_HTTP_URLALWAYS_EQNEVER_EQLARGESTSMALLESTbusy_retry()sleeping_retry()is_resource_enabled()python_is_optimized()with_pymalloc()requires()sortdict()findfile()get_pagesize()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()requires_limited_api()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()SuppressCrashReportSaveSignalsMatcher
test.support.socket_helper--- 用于套接字测试的工具test.support.script_helper--- 用于 Python 执行测试工具test.support.bytecode_helper--- 用于测试正确字节码生成的支持工具test.support.threading_helper--- 用于线程测试的工具test.support.os_helper--- 用于操作系统测试的工具FS_NONASCIISAVEDCWDTESTFNTESTFN_NONASCIITESTFN_UNENCODABLETESTFN_UNDECODABLETESTFN_UNICODEEnvironmentVarGuardFakePathcan_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--- 用于导入测试的工具test.support.warnings_helper--- 用于警告测试的工具