pytest hook function (5): report hook

Preface

The pytest framework provides a lot of hooks. Through these hooks, we can intervene in various stages of pytest use case collection, use case execution, report output, etc., and develop corresponding plug-ins according to needs to meet our own usage scenarios.

01 What is a hook function?

The hook function is called the Hook function in pytest. The developer of the pytest framework has reserved some functions for users to better expand development. These reserved functions will be automatically called and executed at specific stages in the entire test execution life cycle. As shown below:
[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-YIc6C2GC-1689820507301)(/api/attachments/426316)]

The hook functions in pytest are divided into 6 categories according to their functions: boot hooks, initialization hooks, use case collection hooks, use case execution hooks, reporting hooks, and debugging hooks.

For detailed documentation, please view the pytest official documentation https://docs.pytest.org/en/latest/reference/reference.html?highlight=hook#hooks

02 Report hook

2.1 pytest_report_header

This hook function returns a string or list of strings to display as header information for the terminal report.
parameter:

  • config: pytest configuration object.
  • start_path: The starting path of execution.
  • startdir: The starting path of execution (deprecated after version 7.0).

return value:

  • A string or a list containing multiple strings.

2.2 pytest_report_collectionfinish

Returns a string or list of strings to be displayed after use case collection is complete, following the standard "X Use Cases Collected" message.

parameter:

  • config: pytest configuration object.
  • start_path: The starting path of execution.
  • startdir: The starting path of execution (deprecated after version 7.0).
  • items: List of use cases to be executed.

return value:

  • A string or a list containing multiple strings.

2.3 pytest_report_teststatus

This hook function returns the status report's result categories, short letters, and detailed words.

  • Result Category: Is the category of the calculation result, such as Pass, Skip, Error, or the empty string.
  • Short letters: such as ".", "s", "E", or the empty string.
  • Verbose words: For example, "PASSED", "SKIPPED", "ERROR", or the empty string.

parameter:

  • config: pytest configuration object.
  • report : The report object whose status is to be returned.

return value:

  • What is returned is a tuple containing result information, the specific structure Demo: ("Pass", "P", ("Pass", {"res": 'The execution passed'}) ).

Inside the hook function, the information of the incoming report object can be judged and the corresponding result can be returned.

2.4 pytest_report_to_serializable

Serializes the given report object, for example, to JSON formatted data.

parameter:

  • config: pytest configuration object.
  • report : The report object whose status is to be returned.

return value:

  • Any type will do.

2.5 pytest_report_from_serializable

Restore the serialized report data to the report object.

parameter:

  • config: pytest configuration object.
  • report : The report object whose status is to be returned.

return value:

  • Any type will do.

2.6 pytest_terminal_summary

Adds a section to the terminal summary report.

parameter

  • terminalreporter: – Internal terminal reporting object.
  • exitstatus: – The exit status that will be reported back to the operating system.
  • config: – pytest configuration object.

2.7 pytest_fixture_setup

Execute the hook function of the pre-test fixture.

parameter

  • fixturedef
  • request

return value:

  • The return value of the test fixture function.

2.8 pytest_fixture_post_finalizer

Called after the post-fixture is executed but before the cache is cleared.
parameter:

  • fixturedef
  • request

return value:

  • The return value of the test fixture function.

2.9 pytest_warning_recorded

Hook function to capture warning warnings that occur when pytest executes use cases.

parameter:

  • warning_message: captured warning.
  • when
    "config": During the pytest configuration/initialization phase.
    "collect": During test collection.
    "runtest": During test execution.
  • nodeid: ID of the test case
  • location: Saves information (filename, line number, function) about the execution context of the captured warning. function evaluates to when the execution context is at the module level.

2.10 pytest_runtest_logreport

Handle the generation of every setup, call and teardown run test phase of the project's TestReport.

parameter:

  • report: result information of use case execution.

2.11 pytest_assertrepr_compare

The hook function executed when the assertion fails, returns the use case assertion failure and the explanation of the comparison content in the expression.
parameter:

  • config: Configuration object executed by pytest.
  • op: Comparison operator for assertions.
  • left : expected result (content to the left of the comparison operator).
  • right : the actual result (the content on the right side of the comparison operator).

return value:

  • Description of the assertion result (string or list type).

2.12 pytest_assertion_pass

Hook function executed when the assertion passes.

parameter:

  • item: The test case to be executed.
  • lineno: – The line number of the assertion statement.
  • orig :-- String with original assertion.
  • expl: – String with assertion explanation.

Guess you like

Origin blog.csdn.net/FloraCHY/article/details/131824850