開發工具¶
本章所描述的模組可以幫助你編寫軟體。例如 pydoc
模組可以根據模組的內容生成文件;doctest
和 unittest
模組則包含編寫單元測試的框架,這些測試程式碼會自動執行並驗證輸出結果是否正確。
本章節所描述的模組列表為:
typing
--- 支援型別提示- Python 型別系統的技術規範
- 型別別名
- NewType
- 註釋 callable 物件
- 泛型
- 註釋元組 (tuple)
- 類別物件的型別
- Annotating generators and coroutines
- 使用者定義泛型型別
Any
型別- 標稱 (nominal) 子型別 vs 結構子型別
- 模組內容
- 特別型別原語 (primitive)
- 協定
- ABCs for working with IO
- 函式與裝飾器
- Introspection helpers
- 常數
- 棄用的別名
- Deprecation Timeline of Major Features
pydoc
--- 文件產生器與線上幫助系統- Python 開發模式
doctest
--- 測試互動式 Python 範例- Simple Usage: Checking Examples in Docstrings
- Simple Usage: Checking Examples in a Text File
- How It Works
- 基礎 API
- Unittest API
- Advanced API
- Debugging
- Soapbox
unittest
--- 單元測試框架- 簡單範例
- 命令執行列介面 (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
setUp()
tearDown()
setUpClass()
tearDownClass()
run()
skipTest()
subTest()
debug()
assertEqual()
assertNotEqual()
assertTrue()
assertFalse()
assertIs()
assertIsNot()
assertIsNone()
assertIsNotNone()
assertIn()
assertNotIn()
assertIsInstance()
assertNotIsInstance()
assertRaises()
assertRaisesRegex()
assertWarns()
assertWarnsRegex()
assertLogs()
assertNoLogs()
assertAlmostEqual()
assertNotAlmostEqual()
assertGreater()
assertGreaterEqual()
assertLess()
assertLessEqual()
assertRegex()
assertNotRegex()
assertCountEqual()
addTypeEqualityFunc()
assertMultiLineEqual()
assertSequenceEqual()
assertListEqual()
assertTupleEqual()
assertSetEqual()
assertDictEqual()
fail()
failureException
longMessage
maxDiff
countTestCases()
defaultTestResult()
id()
shortDescription()
addCleanup()
enterContext()
doCleanups()
addClassCleanup()
enterClassContext()
doClassCleanups()
IsolatedAsyncioTestCase
FunctionTestCase
- Grouping tests
- Loading and running tests
TestLoader
TestResult
errors
failures
skipped
expectedFailures
unexpectedSuccesses
collectedDurations
shouldStop
testsRun
buffer
failfast
tb_locals
wasSuccessful()
stop()
startTest()
stopTest()
startTestRun()
stopTestRun()
addError()
addFailure()
addSuccess()
addSkip()
addExpectedFailure()
addUnexpectedSuccess()
addSubTest()
addDuration()
TextTestResult
defaultTestLoader
TextTestRunner
main()
- load_tests Protocol
- Test cases
- Class and Module Fixtures
- Signal Handling
unittest.mock
— mock 物件函式庫- 快速導引
- Mock 類別
Mock
assert_called()
assert_called_once()
assert_called_with()
assert_called_once_with()
assert_any_call()
assert_has_calls()
assert_not_called()
reset_mock()
mock_add_spec()
attach_mock()
configure_mock()
__dir__()
_get_child_mock()
called
call_count
return_value
side_effect
call_args
call_args_list
method_calls
mock_calls
__class__
NonCallableMock
PropertyMock
AsyncMock
ThreadingMock
- 呼叫
- 刪除屬性
- Mock 名稱與名稱屬性
- 如同屬性一般附加 mock
- Patchers
- MagicMock 以及魔術方法支援
- 輔助函式
side_effect
、return_value
和 wraps 的優先順序
unittest.mock
--- 入門指南test
--- Python 的回歸測試 (regression tests) 套件test.support
--- Python 測試套件的工具TestFailed
ResourceDenied
verbose
is_jython
is_android
unix_shell
LOOPBACK_TIMEOUT
INTERNET_TIMEOUT
SHORT_TIMEOUT
LONG_TIMEOUT
PGO
PIPE_MAX_SIZE
Py_DEBUG
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
busy_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_gil_enabled()
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()
SuppressCrashReport
SaveSignals
Matcher
test.support.socket_helper
--- 用於 socket 測試的工具test.support.script_helper
--- 用於 Python 執行測試的工具test.support.bytecode_helper
--- 用於測試位元組碼能正確產生的支援工具test.support.threading_helper
--- Utilities for threading teststest.support.os_helper
--- 用於 os 測試的工具FS_NONASCII
SAVEDCWD
TESTFN
TESTFN_NONASCII
TESTFN_UNENCODABLE
TESTFN_UNDECODABLE
TESTFN_UNICODE
EnvironmentVarGuard
FakePath
set()
unset()
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
--- 用於 import 測試的工具test.support.warnings_helper
--- 用於 warnings 測試的工具