Analyzing how pytest works from the perspective of pytest source code
through (a gap)pytest
To analyze how it works from the perspective of the source code, we need to focus on a few key sections, especially thepytest
the startup process and the collection and execution of tests. Below is a description of the process based on thepytest
A high-level overview of the source code.
The pytest startup process
-
Command line parsing.
-
pytest
The entry point to thein the file
()
function. - In this function, it first passes the
.get_config()
Get configuration. - follow with
()
to parse command line arguments.
-
-
Configuration loading.
-
pytest
will recursively look for configuration files in the current directory and its parent directory, for examplemaybe
。
- utilization
class to store configuration information.
-
-
Plugin Management.
- pass (a bill or inspection etc)
cap (a poem)
to manage plugins.
- Plugins can be registered and called at various stages.
- pass (a bill or inspection etc)
Test collection process
-
Collector initialization.
-
pytest
utilizationmodule to handle test collection.
-
Session.from_parent
method creates a newSession
Example. -
Collector.from_parent
method is used to build the collector tree.
-
-
Test file found.
-
pytest
pass (a bill or inspection etc)Session.perform_collect
method to traverse the directory structure and discover test modules. -
File.from_parent
method is used to create theFile
instance to represent the test file. -
Function.from_parent
method is used to create theFunction
instance to represent the test function.
-
-
Test Item Construction.
- Once the test file has been found, it is passed through the
collect
method to collect the test functions in the file. - The test function is converted to
Item
Example.
- Once the test file has been found, it is passed through the
Test execution process
-
Test item preparation.
- Before the test starts, the
Session.perform_setup
method to do some preprocessing. - This phase may include setting environment variables, initializing database connections, etc.
- Before the test starts, the
-
Test item implementation.
-
Methods control the actual execution of test items.
- For each
Item
instances, both of which call theSession.perform_test
method to execute the test.
-
-
Collection of test results.
- The results of the test execution are collected and stored in the
Item
Example. - May be triggered
pytest_runtest_logreport
hook, which is used to process the test reports.
- The results of the test execution are collected and stored in the
-
Exception Handling.
- If an exception occurs during testing, the
pytest
will catch these exceptions and log them. - Exceptions can be passed through the
pytest_runtest_makereport
hook to handle it.
- If an exception occurs during testing, the
Test Report Generation
-
Session
instance is responsible for collecting all test results. -
attribute will determine the exit status code of the program based on the test results.
-
pytest
Reports can be generated in a variety of formats, depending on the plug-in installed.
Sample Code Snippet
Here are some sample code snippets that show how thepytest
Key parts of the source code:
# pytest/ def main(args=None): # Parsing command line arguments config = get_config(args) # Loading Plug-ins pm = PluginManager() pm.load_setuptools_entrypoints('pytest11') # Creating a Session Instance session = Session.from_parent(config, plugins=pm) # execute a test () # Return to Exit Status return # pytest/ def perform_collect(session, collector): # Collecting test files and test functions items = [] for item in (): (item) return items # pytest/ def runtest_protocol(item, nextitem): # Execution of test items report = () if report is None: # Handling of anomalies report = () # Processing of test reports ._hookmanager.hook.pytest_runtest_logreport(report=report)