開發工具¶
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
--- 支援型別提示- 相關的 PEPs
- 型別別名
- NewType
- 註釋 callable 物件
- 泛型
- 註釋元組 (tuple)
- The type of class objects
- User-defined generic types
Any
型別- Nominal vs structural subtyping
- 模組內容
- Special typing primitives
- 協定
- ABCs for working with IO
- 函式與裝飾器
- Introspection helpers
- 常數
- 棄用的別名
- Deprecation Timeline of Major Features
pydoc
--- Documentation generator and online help system- Python Development Mode
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
--- 單元測試框架- 簡單範例
- 命令執行列介面 (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
- 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.collectedDurations
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()
TestResult.addDuration()
TextTestResult
defaultTestLoader
TextTestRunner
main()
- load_tests Protocol
- Test cases
- Class and Module Fixtures
- Signal Handling
unittest.mock
— mock 物件函式庫- 快速導引
- Mock 類別
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
- 呼叫
- Deleting Attributes
- Mock names and the name attribute
- Attaching Mocks as Attributes
- The patchers
- MagicMock and magic method support
- Helpers
unittest.mock
--- 入門指南- 使用 Mock 的方式
- 使用 Mock 來 patching 方法
- 對物件的方法呼叫使用 mock
- 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
- Using side_effect to return per file content
- 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
- 使用 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
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 測試的工具