開發工具¶
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
--- 支援型別提示- 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
- 呼叫
- 刪除屬性
- Mock 名稱與名稱屬性
- 如同屬性一般附加 mock
- Patchers
- MagicMock 以及魔術方法支援
- 輔助函式
- Order of precedence of
side_effect
,return_value
and wraps
unittest.mock
--- 入門指南- 2to3 --- 自動將 Python 2的程式碼轉成 Python 3
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_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 測試的工具