将您的字段添加到Pytest报告中

当与pytest一起使用时,面对修改html报告的任务,因此,我找到了一个对我的任务方便的解决方案,我想与他人分享-也许有人会派上用场。*所有图片均可点击




操作需要以下组件:
* 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-

项目结构
|
-test.py (主测试脚本)|-conftest.py(实现了钩子脚本的本地插件|-test
-1.jpg(图像文件)
| -test -2.jpg(文件图片)

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
 


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


该报告的修改在文件conftest.py中进行


通过以下函数创建新列:pytest_html_results_table_header(单元格)pytest_html_results_table_row(报告,单元格)

编码
#   
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)) -将一列插入_name下的表中,其中包含_name的内容

拦截测试执行并填写测试报告是通过装饰器@ pytest.hookimpl(hookwrapper = True)通过函数钩子:
def pytest_runtest_makereport(项目,调用)完成的

编码
# 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


那些。执行测试脚本时,将执行pytest_runtest_makereport(项目,调用)函数,该函数依次是:

  • report.description = str(item.function .__ doc__) - 将为report.description列分配测试函数的__doc__属性的值;
  • report.a = str(item.module.log ['a']) -将为report.a列分配全局变量log ['a']的值;
  • report.b = str(item.module.log ['b']) -将为report.b列分配全局变量log ['b']的值;
  • report.sum = str(item.module.log ['sum']) -将为report.sum列分配全局变量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.


参数通过全局变量和__doc__函数属性传输到报表;在每次测试期间,我们必须重新定义全局变量。

编码
#  
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()


要运行测试,请转到带有测试脚本的目录并运行以下命令:

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

作为测试的结果,将创建report.html文件

测试结果:


  1. test_default()
    使用默认参数进行测试:

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



  2. test_1()
    测试为全局变量分配值并检查求和结果。

    编码
    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.:


在这种情况下,研究了通过全局变量将参数传递到报表的示例,对于我的任务,这是完全可以接受的。但是,使用全局变量会带来已知的困难和问题,因此请小心。

希望本文对您有所帮助。

另一个报告示例


All Articles