Agregar sus campos a un informe de Pytest

Enfrentado la tarea de modificar el informe html al trabajar con pytest, como resultado de lo cual encontré una solución conveniente para mi tarea, quiero compartirla; tal vez alguien sea útil. * Todas las imágenes son clicables




Los siguientes componentes son necesarios para la operación:
* python3 ( Linux: apt-get install python3 ) — Python
* pip3 ( Linux: apt-get install python3-pip ) — Python ( )
* pytest ( Linux: pip3 install pytest ) — framework
* pytest-html ( Linux: pip3 install pytest-html ) — pytest html-

Estructura del proyecto :
| - test.py (Script de prueba principal)
| - conftest.py (Plug-in local donde se implementan scripts de enlace)
| - test-1.jpg (archivo de imagen)
| - test-2.jpg (archivo con imagen)

El contenido de test.py:
import pytest
import base64

#  
log = { 'a':'none', 'b':'none', 'sum':'none' }
log_html = 'none'
log_img = 'none'
log_img_url = 'none'

#    
def set_global_var(a='none',b='none',sum_='none',html='none',img='none',img_url='none'):
	global log, log_html, log_img, log_img_url
	log = { 'a': a, 'b': b, 'sum': sum_ }
	log_html = html
	log_img = img
	log_img_url = img_url

# ,  ,        
@pytest.fixture(scope="function", autouse=True)
def default_global_var():
	set_global_var()

def test_default():
	'''    '''
	assert True

def test_1():
	''' '''
	#  
	a, b = 2, 2
	sum_ = a + b
	#    
	set_global_var( a = a, b = b, sum_ = sum_ )
	# 
	assert sum_ == 4

def test_2():
	''' '''
	#  
	a, b = 3, 3
	sum_ = a + b
	#  html 
	html = '''
	<p>
	  <table border="3" width="100%">
	  <tbody>
	    <tr>
	      <td bgcolor="#D3D3D3"><font color="black"><strong>a</strong></font></td>
	      <td bgcolor="#D3D3D3"><font color="black"><strong>b</strong></font></td>
	      <td bgcolor="#D3D3D3"><font color="black"><strong>sum</strong></font></td>
	    </tr>
	    <tr>
	      <td><font color="black">{0}</font></td>
	      <td><font color="black">{1}</font></td>
	      <td><font color="black">{2}</font></td>
	    </tr>
	  </tbody>
	  </table>
	  </p>
	  '''.format( a, b, sum_ )
	#    
	set_global_var( a = a, b = b, sum_ = sum_, html = html )
	# 
	assert sum_ == 6

def test_3():
	''' '''
	#  
	a, b = 4, 4
	sum_ = a + b
	#     base64
	image = open('test-1.jpg', 'rb')
	image_read = image.read()
	img = base64.encodebytes( image_read ).decode("utf-8")
	#    
	set_global_var( a = a, b = b, sum_ = sum_, img = img )
	# 
	assert sum_ == 8

def test_4():
	''' '''
	#  
	a, b = 5, 5
	sum_ = a + b
	#    url
	img_url = 'test-2.jpg'
	#    
	set_global_var( a = a, b = b, sum_ = sum_, img_url = img_url )
	# 
	assert sum_ == 10

def test_5():
	''' '''
	#  
	a, b = 5, 1
	sum_ = a + b
	#  html 
	html = '<h1> HTML-</h1>'
	#     base64
	image = open('test-1.jpg', 'rb')
	image_read = image.read()
	img = base64.encodebytes( image_read ).decode("utf-8")
	#    url
	img_url = 'test-2.jpg'
	#    
	set_global_var( a = a, b = b, sum_ = sum_, html = html, img = img, img_url = img_url )
	# 
	print ('    ...')
	assert sum_ == 6

#   
@pytest.mark.parametrize("test_a, test_b, test_sum", [(1,2,3), (2,3,5), (4,5,8)])
def test_6(test_a, test_b, test_sum):
	'''  '''
	#  
	a, b = test_a, test_b
	sum_ = a + b
	#    
	set_global_var( a = a, b = b, sum_ = sum_ )
	# 
	assert sum_ == test_sum
 


Contenido de conftest.py:
import pytest
from py.xml import html

#   
def pytest_html_results_table_header(cells):
	cells.insert(1, html.th('description'))		#  1- 
	cells.insert(2, html.th('a'))			#  2- 
	cells.insert(3, html.th('b'))			#  3- 
	cells.insert(4, html.th('sum'))			#  4- 
	cells.pop()

def pytest_html_results_table_row(report, cells):
	cells.insert(1, html.td( report.description ))	#  1-    
	cells.insert(2, html.td( report.a ))		#  2-    
	cells.insert(3, html.td( report.b ))		#  3-    
	cells.insert(4, html.td( report.sum ))		#  4-    
	cells.pop()

# hook       
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
	pytest_html = item.config.pluginmanager.getplugin('html')
	outcome = yield
	report = outcome.get_result()
	#     -     __doc__    
	report.description = str( item.function.__doc__ )
	report.a = str( item.module.log['a'] )
	report.b = str( item.module.log['b'] )
	report.sum = str( item.module.log['sum'] )
	#  html  image    -     
	extra = getattr(report, 'extra', [])
	if report.when == 'call':
		#  html-
		if item.module.log_html != 'none':
			extra.append(pytest_html.extras.html( item.module.log_html ))
		#  img- (    )
		if item.module.log_img != 'none':
			extra.append(pytest_html.extras.image( item.module.log_img, mime_type='image/jpg', extension='jpg' ))
		#  img-  url-
		if item.module.log_img_url != 'none':
			extra.append(pytest_html.extras.image( item.module.log_img_url ))
		report.extra = extra


La modificación del informe se realiza en el archivo conftest.py


La creación de nuevas columnas se realiza a través de las funciones: pytest_html_results_table_header (celdas) y pytest_html_results_table_row (informe, celdas) :

El código
#   
def pytest_html_results_table_header(cells):
	cells.insert(1, html.th('description'))		#  1- 
	cells.insert(2, html.th('a'))			#  2- 
	cells.insert(3, html.th('b'))			#  3- 
	cells.insert(4, html.th('sum'))			#  4- 
	cells.pop()

def pytest_html_results_table_row(report, cells):
	cells.insert(1, html.td( report.description ))	#  1-    
	cells.insert(2, html.td( report.a ))		#  2-    
	cells.insert(3, html.td( report.b ))		#  3-    
	cells.insert(4, html.td( report.sum ))		#  4-    
	cells.pop()


  • cells.insert ( _number , html.th ( _name )) - insertos de una columna en la tabla bajo _number con el contenido de _name

La intercepción de la ejecución de la prueba y el llenado del informe de prueba se realiza a través de la función hook:
def pytest_runtest_makereport (item, call) con el decorador: @ pytest.hookimpl (hookwrapper = True) .

El código
# hook       
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
	pytest_html = item.config.pluginmanager.getplugin('html')
	outcome = yield
	report = outcome.get_result()
	#     -     __doc__    
	report.description = str( item.function.__doc__ )
	report.a = str( item.module.log['a'] )
	report.b = str( item.module.log['b'] )
	report.sum = str( item.module.log['sum'] )
	#  html  image    -     
	extra = getattr(report, 'extra', [])
	if report.when == 'call':
		#  html-
		if item.module.log_html != 'none':
			extra.append(pytest_html.extras.html( item.module.log_html ))
		#  img- (    )
		if item.module.log_img != 'none':
			extra.append(pytest_html.extras.image( item.module.log_img, mime_type='image/jpg', extension='jpg' ))
		#  img-  url-
		if item.module.log_img_url != 'none':
			extra.append(pytest_html.extras.image( item.module.log_img_url ))
		report.extra = extra


Aquellos. cuando se ejecuta el script de prueba, se ejecutará la función pytest_runtest_makereport (item, call) , que a su vez:

  • report.description = str (item.function .__ doc__) - a la columna report.description se le asignará el valor del atributo __doc__ de la función de prueba;
  • report.a = str (item.module.log ['a']) - a la columna report.a se le asignará el valor de la variable global log ['a'];
  • report.b = str (item.module.log ['b']) - a la columna report.b se le asignará el valor de la variable global log ['b'];
  • report.sum = str (item.module.log ['sum']) : a la columna report.sum se le asignará el valor de la variable global log ['sum'];
  • extra.append(pytest_html.extras.html( item.module.log_html )) — html-, log_html;
  • extra.append(pytest_html.extras.image( item.module.log_img, mime_type='image/jpg', extension='jpg' )) — img-, log_img;
  • extra.append(pytest_html.extras.image( item.module.log_img_url )) — img-, log_img_url.

test.py.


Los parámetros se transfieren al informe a través de variables globales y el atributo de función __doc__; durante cada prueba, debemos redefinir las variables globales.

El código
#  
log = { 'a':'none', 'b':'none', 'sum':'none' }
log_html = 'none'
log_img = 'none'
log_img_url = 'none'


Asignaremos valores a variables globales a través de la función:

El código
#    
def set_global_var(a='none',b='none',sum_='none',html='none',img='none',img_url='none'):
	global log, log_html, log_img, log_img_url
	log = { 'a': a, 'b': b, 'sum': sum_ }
	log_html = html
	log_img = img
	log_img_url = img_url


Para restablecer automáticamente los valores de las variables globales y no controlarlos en cada prueba, usamos fixture.

El código
@pytest.fixture(scope="function", autouse=True)
def default_global_var():
	set_global_var()


Para ejecutar la prueba, vaya al directorio con el script de prueba y ejecute el comando:

pytest test.py -v --html=report.html --self-contained-html

Como resultado de la prueba, se creará el archivo report.html .

Resultado de la prueba:


  1. test_default ()
    Prueba con parámetros predeterminados:

    El código
    def test_default():
    	'''    '''
    	assert True
    



  2. test_1 ()
    La prueba asigna valores a las variables globales y verifica el resultado de la suma.

    El código
    def test_1():
    	''' '''
    	#  
    	a, b = 2, 2
    	sum_ = a + b
    	#    
    	set_global_var( a = a, b = b, sum_ = sum_ )
    	# 
    	assert sum_ == 4
    



  3. test_2()

    ( html-) .

    def test_2():
    	''' '''
    	#  
    	a, b = 3, 3
    	sum_ = a + b
    	#  html 
    	html = '''
    	<p>
    	  <table border="3" width="100%">
    	  <tbody>
    	    <tr>
    	      <td bgcolor="#D3D3D3"><font color="black"><strong>a</strong></font></td>
    	      <td bgcolor="#D3D3D3"><font color="black"><strong>b</strong></font></td>
    	      <td bgcolor="#D3D3D3"><font color="black"><strong>sum</strong></font></td>
    	    </tr>
    	    <tr>
    	      <td><font color="black">{0}</font></td>
    	      <td><font color="black">{1}</font></td>
    	      <td><font color="black">{2}</font></td>
    	    </tr>
    	  </tbody>
    	  </table>
    	  </p>
    	  '''.format( a, b, sum_ )
    	#    
    	set_global_var( a = a, b = b, sum_ = sum_, html = html )
    	# 
    	assert sum_ == 6
    



  4. test_3()
    ( img-) .

    def test_3():
    	''' '''
    	#  
    	a, b = 4, 4
    	sum_ = a + b
    	#     base64
    	image = open('test-1.jpg', 'rb')
    	image_read = image.read()
    	img = base64.encodebytes( image_read ).decode("utf-8")
    	#    
    	set_global_var( a = a, b = b, sum_ = sum_, img = img )
    	# 
    	assert sum_ == 8
    




    :


  5. test_4()

    ( img-) .

    def test_4():
    	''' '''
    	#  
    	a, b = 5, 5
    	sum_ = a + b
    	#    url
    	img_url = 'test-2.jpg'
    	#    
    	set_global_var( a = a, b = b, sum_ = sum_, img_url = img_url )
    	# 
    	assert sum_ == 10
    




    :


  6. test_5()

    ( html-, img-) .

    def test_5():
    	''' '''
    	#  
    	a, b = 5, 1
    	sum_ = a + b
    	#  html 
    	html = '<h1> HTML-</h1>'
    	#     base64
    	image = open('test-1.jpg', 'rb')
    	image_read = image.read()
    	img = base64.encodebytes( image_read ).decode("utf-8")
    	#    url
    	img_url = 'test-2.jpg'
    	#    
    	set_global_var( a = a, b = b, sum_ = sum_, html = html, img = img, img_url = img_url )
    	# 
    	print ('    ...')
    	assert sum_ == 6
    



  7. test_6()

    , .

    #

    @pytest.mark.parametrize("test_a, test_b, test_sum", [(1,2,3), (2,3,5), (4,5,8)])
    def test_6(test_a, test_b, test_sum):
    	'''  '''
    	#  
    	a, b = test_a, test_b
    	sum_ = a + b
    	#    
    	set_global_var( a = a, b = b, sum_ = sum_ )
    	# 
    	assert sum_ == test_sum
    




P.S.:


En este caso, se examinó un ejemplo de pasar parámetros a un informe a través de variables globales, para mi tarea esto es bastante aceptable. Pero el uso de variables globales impone dificultades y problemas conocidos, así que tenga cuidado.

Espero que este artículo te sea útil.

Otro ejemplo de informe


All Articles