Web automated testing (advanced learning part 2)

Table of contents

1. Automated Assertions

1.1 Assertion concept

1.2 Introduction to Assertions

1.3 Code case

2. Automated reporting

2.1HTMLTestRunner

2.1.1 Download and installation of HTMLTestRunner

2.1.2HTMLTestRunner use

2.2BeautifulReport

2.2.1 Download and installation of BeautifulReport

2.2.2 BeautifulReport using steps

2.2.3 Code case

2.3pytest-Allure

2.3.1 Allure download and installation

2.3.2 Allure view and generate reports

2.3.3 Code case

2.3.4 allure feature

1. Automated Assertions

1.1 Assertion concept

       Based on the automated testing of selenium+python, the assertion is assert (provided by the TestCase class of the unittest framework). If the assertion execution fails, an exception will be thrown. If our expected result is not achieved at this time, the test case will fail; if the execution succeeds, our expected result will be achieved. In short, the assertion is to compare our expected results with the actual results, and if they are equal, the test case passes.

1.2 Introduction to Assertions

  • assertEqual(a,b, [msg='information printed when the test fails']): Assert whether a and b are equal.
  • assertNotEqual(a,b, [msg='information printed when the test fails']): Assert whether a and b are equal, and pass if they are not equal.
  • assertTrue(x, [msg='information printed when the test fails']): Assert whether x is True, if it is, the test case passes.
  • assertFalse(x, [msg='information printed when the test fails']): Assert whether x is False, if it is, the test case passes.
  • assertIs(a,b, [msg='information printed when the test fails']): Assert whether a is b, if yes, the test case passes.
  • assertNotIs(a,b, [msg='information printed when the test fails']): Assert whether a is b, if not, the test case passes.
  • assertIsNone(x, [msg='information printed when the test fails']): Assert whether x is None, if it is None, pass.
  • assertIsNotNone(x, [msg='information printed when the test fails']): Assert whether x is None, if it is not None, pass.
  • assertIn(a,b, [msg='information printed when the test fails']): Assert whether a is in b, if in b, the test case passes.
  • assertNotIn(a,b, [msg='information printed when the test fails']): Assert whether a is in b, if not in b, pass.
  • assertIsInstance(a,b, [msg='information printed when the test fails']): Assert whether a is an instance of b, if so, the test case passes.
  • assertNotIsInstance(a,b, [msg='information printed when the test fails']): Assert whether a is an instance of b, if not, the test case passes.

1.3 Code case

import unittest
class Test(unittest.TestCase):
	def setUp(self):
		pass
	def test_case(self):
		self.a = "hello"
		self.b = "hello world"
		self.assertIn(self.a,self.b,msg="a is in b")
	def tearDown(self):
		pass
if __name__ == "__main__":
	unittest.main()

2. Automated reporting

2.1HTMLTestRunner

2.1.1 Download and installation of HTMLTestRunner

Download address: HTMLTestRunner download

Installation: HTMLTestRunner is an independent HTMLTestRunner.py file, which can be used either as a third-party library of Python or as part of a project. Just put the HTMLTestRunner.py file in the python installation directory, such as: D:\install\python\Lib\site-packages

2.1.2HTMLTestRunner use

Partial implementation of the HTMLTestRunner class, parameters of the __init__() initialization method:

  • stream: Specify the file to generate HTML test report, required.
  • verbosity: Specifies the level of the log, the default is 1. If you want to get more detailed logs, you can modify the parameter to 2.
  • title: Specify the title of the test case, the default is None.
  • description: Specify the description of the test case, the default is None

2.1.3 Code case

         Create addition, subtraction, multiplication, and division in a simple calculator program and write unit test codes to generate test reports. The project hierarchy is as follows:

 Caculator.py code

# 简易计算器程序中的加、减、乘、除代码。

class Caculator:
    def add(self, a, b):
        return a + b

    def substract(self, a, b):
        return a - b

    def multi(self, a, b):
        return a * b

    def divide(self, a, b):
        return a / b

 test_Caculator.py code

import unittest
from com.test_sanmu.caculator import Caculator


class CaculatorTest(unittest.TestCase):
    @classmethod
    def setUp(self) -> None:
        self.cacylator = Caculator
    def test_caculator1(self):
        self.assertEqual(5,self.cacylator.substract(self,6,1))
        self.assertEqual(3,self.cacylator.multi(self,1,3))
        self.assertEqual(5,self.cacylator.divide(self,10,2))
        self.assertEqual(6,self.cacylator.add(self,3,3))
    @classmethod
    def tearDown(self) -> None:
        print("断言结束")

if __name__ == '__main__':
      unittest.main()

reporter.py 

'''
执行多个测试文件
discover(start_dir,pattern='test*.py',top_level_dir=None)
start_dir :待测试的模块名或测试用例目录。
pattern='test*.py' :测试用例文件名的匹配原则。此处匹配文件名以“test”开头
的“.py”类型的文件,星号“*”表示任意多个字符。
top_level_dir=None:测试模块的顶层目录,如果没有顶层目录,则默认为 None。
'''
import os
import unittest
import HTMLTestRunner
import time

# 用例路径
case_path = os.path.join( os.getcwd(), 'test_sanmu' )
# 报告存放路径
report_path = os.path.join( os.getcwd(), 'reports' )

if not os.path.exists( report_path ):
    os.mkdir( report_path )


def all_case():
    suits = unittest.defaultTestLoader.discover( case_path, pattern="test*.py", top_level_dir=None )
    return (suits)


if __name__ == '__main__':
    # 1、获取当前时间,便于生成报告,不易被覆盖。
    now_time = time.strftime( "%Y-%m-%d-%H_%M_%S", time.localtime( time.time() ) )
    # 2、html报告文件路径
    report_abspath = os.path.join( report_path, "result_" + now_time + ".html" )
    # 3、打开一个文件,将result写入此file中
    fp = open( report_abspath, "wb" )
    runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title='测试报告', description="运行环境:Sanmu Windows 10, Chrome 浏览器",verbosity=2 )
    # 4、调用add_case函数返回值
    runner.run( all_case() )
    fp.close()

Generate a report of the results 

2.2BeautifulReport

       BeautifulReport is a reporting framework for generating automated test reports in HTML format based on the Unittest testing framework. It is a simple and beautiful HTML template. Add the generated results to the template and generate an HTML format report in the specified directory.

2.2.1 Download and installation of BeautifulReport

Download link: Download BeautifunReport

Installation: After downloading the zip format, unzip it, and load the entire file into the python installation directory, such as: D:\install\python\Lib\site-packages

2.2.2 BeautifulReport using steps

1. Assemble the test suite: Put the test cases to be executed into the test suite
2. Prepare the test report file (path + report file name.html) 
3. Execute the test cases in batches and write the results into the test report file.

Description of report parameters:

  • 1. filename The file name of the report
  • 2. description: Notes for generated files
  • 3. report_dir: file storage path to generate report 
  • 4. theme: report theme

2.2.3 Code case

The test case is the same as above, Caculator.py implementation and test_Caculator.py unit test, the generated report code is as follows:

import time
import unittest
import os
from BeautifulReport import BeautifulReport

# 用例路径
case_path = os.path.join( os.getcwd(), 'test_sanmu' )
# 报告存放路径
report_path = os.path.join( os.getcwd(), 'reports' )

if not os.path.exists( report_path ):
    os.mkdir( report_path )

suits = unittest.defaultTestLoader.discover(case_path, 'test*.py', None)
now_time = time.strftime( "%Y-%m-%d-%H_%M_%S", time.localtime( time.time() ) )
filename = 'Sanmu测试报告' + str(now_time)
BeautifulReport(suits).report(filename=filename,description='beautifual报告',log_path=report_path)

 Generate a report of the results

2.3pytest-Allure

      Allure is a powerful reporting framework, combined with the Pytest unit testing framework, it can display the execution of test cases in detail, and display various unique analysis results. It also supports switching between multiple languages. It is the most powerful so far, but The operation is relatively complicated.

2.3.1 Allure download and installation

Download link: Allure download

Installation: Because Allure is developed in java language, make sure that your environment has jdk1.8 version and above, then unzip the file -> enter the bin directory -> run allure.bat -> add the bin directory to the PATH environment variable -> cmd input allure --version to verify whether the configuration is successful -> install the plugin: pip install allure-pytest

2.3.2 Allure view and generate reports

(1) Generate: pytest [test file] --alluredir=./result #--alluredir is used to specify the path to store test results

(2) View: a. Directly open the default browser to display the report:allure serve ./result/

                    b. Generate a report from the results: generate a report, allure generate ./result/ -o ./report/ --clean (coverage path plus --clean) -> open reportallure open -h [生成文件的IP] -p [生成文件的端口] ./report/,例如我的是:10.7.129.39:42035

2.3.3 Code case

import allure
import pytest
from selenium import webdriver
import time

from selenium.webdriver.common.by import By


@allure.feature( "百度搜索" )
@pytest.mark.parametrize( 'test_data', ['allure', 'pytest', 'Sanmu'] )#参数化
def test_steps_demo(test_data):
    with allure.step( "打开百度网页" ):
        driver = webdriver.Chrome()
        driver.get( "http://www.baidu.com" )

    with allure.step( "搜索" ):
        driver.find_element( By.ID, "kw" ).send_keys( test_data )
        time.sleep( 2 )
        driver.find_element( By.ID, "su" ).click()
        time.sleep( 2 )

    with allure.step( "保存图片" ):
        driver.save_screenshot( "./result/result.png" )
        allure.attach.file( "./result/result.png", attachment_type=allure.attachment_type.PNG )
        allure.attach( '<head></head><body>首页</body>', 'Attach with HTML type', allure.attachment_type.HTML )

    with allure.step( "退出浏览器" ):
        driver.quit()

 run:

 Generate the report as follows:

2.3.4 allure feature

  • @allure.feature('feature name'): equivalent to testsuite
  • @allure.story('sub-function name'): corresponding to different scenarios under this function or module, which is equivalent to testcase
  • @allure.step('step'): Each step in the test process is placed in a specific logic method
    • allure.step('step') : can only be placed on a class or method in the form of a decorator
    • with allure.step: can be placed in the test case method
  • @allure.attach('specific text information'): data, text, picture, video, web page
  • @link, @issue, @testcase: You can add links, bug addresses, and test case addresses in the test report.
  • @allure.severity is marked by importance level, there are 5 levels:
    • Blocker level: blocking
    • Critical level: serious
    • Normal level: normal
    • Minor level: not very important
    • Trivial level: not important

Guess you like

Origin blog.csdn.net/m0_58807719/article/details/130048410