pytest: Best Way To Add Long Test Description in the Report

2024/11/15 16:58:10

By default pytest use test function names or test files names in pytest reports

is there any Best way to add test description (Long test name) in the report with out renaming the files or functions using pytest?

Can we do this by updating the testcase name at run-time like ?

  1. request.node.name
request.node.name = "Very Very Very Very Very long long long long name name name name"
  1. Description after test-name
def test_ok():
"""Very Very Very Very Very long long long long name name name name"""print("ok")
Answer

Using the pytest_runtest_makereport hook, the reported name can be adjusted for each test. (Note that hooks must be placed within a plugin, or a conftest.py)

# conftest.pyimport pytest@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):outcome = yieldreport = outcome.get_result()test_fn = item.objdocstring = getattr(test_fn, '__doc__')if docstring:report.nodeid = docstring# test_it.pydef test_ok():"""Very Very Very Very Very long long long long name name name name"""print("ok")

This will produce output similar to:

tests/test_stuff.py::test_ok 
Very Very Very Very Very long long long long name name name name <- tests/test_stuff.py PASSED [100%]

See "hookwrapper: executing around other hooks" for more info on the outcome = yield and outcome.get_result() business.

https://en.xdnf.cn/q/71868.html

Related Q&A

Adding a calculated column to pandas dataframe

I am completely new to Python, pandas and programming in general, and I cannot figure out the following:I have accessed a database with the help of pandas and I have put the data from the query into a …

Scipy: Centroid of convex hull

how can I calculate the centroid of a convex hull using python and scipy? All I found are methods for computing Area and Volume.regards,frank.

Creating a montage of pictures in python

I have no experience with python, but the owner of this script is not responding.When I drag my photos over this script, to create a montage, it ends up cutting off half of the last photo on the right …

stop python program when ssh pipe is broken

Im writing a python script with an infinite while loop that I am running over ssh. I would like the script to terminate when someone kills ssh. For example:The script (script.py):while True:# do someth…

How do I export a TensorFlow model as a .tflite file?

Background information:I have written a TensorFlow model very similar to the premade iris classification model provided by TensorFlow. The differences are relatively minor: I am classifying football ex…

Using plotly in Jupyter to create animated chart in off-line mode

Ive been trying to get the "Filled-Area Animation in Python" example to work using plotly in offline mode in a Jupyter notebook. The example can be found here: https://plot.ly/python/filled-a…

Django: How to unit test Update Views/Forms

Im trying to unit test my update forms and views. Im using Django Crispy Forms for both my Create and Update Forms. UpdateForm inherits CreateForm and makes a small change to the submit button text. Th…

Why is Python faster than C++ in this case?

A program in both Python and C++ is given below, which performs the following task: read white-space delimited words from stdin, print the unique words sorted by string length along with a count of eac…

Python - write headers to csv

Currently i am writing query in python which export data from oracle dbo to .csv file. I am not sure how to write headers within file. try:connection = cx_Oracle.connect(user,pass,tns_name)cursor = con…

Opening/Attempting to Read a file [duplicate]

This question already has answers here:PyCharm shows unresolved references error for valid code(31 answers)Closed 5 years ago.I tried to simply read and store the contents of a text file into an array,…