Test Report (Advanced)

Functional testing can write a test report by hand

1. How to automatically generate test reports

unittest generates test reports

Test case: account is correct, password is wrong

Enter a set of account numbers according to the test case, click login, and a message will appear, the password is wrong

code:

import unittest
import time
from selenium import webdriver
from selenium.webdriver.common.by import By

# 必须要继承unittest.TestCase
class TestCase(unittest.TestCase):

    # setUP 用例执行前要执行的一个方法
    def setUp(self) -> None:
        # 登录的准备工作:打开浏览器,访问登录页
        # 通过浏览器驱动打开谷歌浏览器
        self.driver = webdriver.Chrome()
        # 访问登录页
        self.driver.get('http://39.98.138.157/shopxo/index.php?s=/index/user/logininfo.html')

    # tearDown 用例执行之后要执行的一个方法
    def tearDown(self) -> None:
        # 关闭浏览器,一般会等待三秒钟关闭浏览器
        # alt+enter 自动导包
        time.sleep(4)
        self.driver.quit()

    # 测试过程  测试方法  一条测试用例就是一条test 一个方法必须以test开头
    # 用户名正确。密码不正确
    def test_01(self):
        # 找到输入框,输入用户名
        # 找到输入框,输入密码
        # 找到登录按钮并点击登录
        self.driver.find_element(By.XPATH, '/html/body/div[4]/div/div[2]/div[2]/form/div[1]/input').send_keys('666666')
        self.driver.find_element(By.XPATH, '/html/body/div[4]/div/div[2]/div[2]/form/div[2]/input').send_keys('633333')
        self.driver.find_element(By.XPATH, '/html/body/div[4]/div/div[2]/div[2]/form/div[3]/button').click()
        # 预期结果
        # expected='密码错误'
        # 做断言
        # 一般而言使用if expected ==
        # unittest提供了断言
        time.sleep(1)
        msg=self.driver.find_element(By.XPATH, '//*[@id="common-prompt"]/p').text
        self.assertEqual('密码错误', msg)

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

Considerations: Assert --Find Password Errors

2. Optimize the automatic generation of test reports unittest

Pytest is deepened on the basis of unittest, it will be easier to use, and the difficulty of learning will be higher

1. unittest generates a test report

To solve the problem, import the os package,

Reference document: ​​​​Error message: ImportError: Start directory is not importable: 'D:\\PyCharm-Workspace\\testcases' - 喵喵 kmm - 博客园

 correct answer:

Description: testCase20211214 is the root directory

        The console output is also an alternative test report, but it is not comprehensive and difficult to understand

2. unittest has a unique method of generating text test reports

 

The above problem is mainly because the path is written wrong, directly find the root directory to create the file

correct answer:

running result:

The above test_01, test_02 will open a new browser every time it is executed

3. Plug-in, html testrunner installation

html testrunner is an upgrade for the test report automatically generated by unittest

1) Download htmlTestRunner

HTMLTestRunner - tungwaiyip's software

Note: If there is a problem with htmltestrunner, you can modify it according to the following modification plan

htmlTestRunner.py only supports python2, python3 is not compatible with python2

2) Check the python version

3) Put htmltestrunner under lib

4) Use HtmlTestRunner.py to implement the test report

5) Running effect: the html file should be opened with a browser

6) There is a problem, the operation fails, and the test report does not appear

  1. 6.1) Check if the HtmlTestRunner.py file is wrong

Python+Selenium+HTMLTestRunner automatically generates a test report, but the solution is blank when it is opened

Reference documents:

Python+Selenium+HTMLTestRunner automatically generates a test report, but the solution is blank when it is opened - Wanqing lazy blog - CSDN blog

In the process of testing, the most important step is the generation of test reports, and automated testing also needs to automatically generate test reports. But unittest cannot generate reports in html format, which requires importing a third-party module: HTMLTestRunner.

One: Download HTMLTestRunner

Download path: https://pypi.python.org/pypi/HTMLTestRunner, after downloading, put it in the Python installation directory\Lib directory.

The HTMLTestRunner.py file under Download is the package we need to download. If you can only open this file after clicking, you can copy the file content into Notepad and save it as HTMLTestRunner.py.

2. Modify the HTMLTestRunner.py file

If you installed the python2 version, you don't need to do this step. Because HTMLTestRunner.py is originally the python2 version, and the python3 version has not been found yet, so we need to modify the HTMLTestRunner.py file ourselves.

The modification is as follows:

Line 94, modify import StringIO to import io

Line 539, modify self.outputBuffer = StringIO.StringIO() to self.outputBuffer = io.StringIO()

Line 642, change if not rmap.has_key(cls): to if not cls in rmap:

Line 766, change uo = o.decode('latin-1') to uo = e

Line 774, change ue = e.decode('latin-1') to ue = e

Line 631, change print >> sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime) to print(sys.stderr, '\nTime Elapsed: %s' % (self. stopTime-self.startTime))

Among them, in Python3, there is no StringIO anymore. Instead, use io.StringIO.

  1. 6.2) After confirming that the modification of htmltestrunner is correct, continue to run and generate a report, but it is still blank when opened

Possible reason one: the file is not closed, just add the code fp.close().

Possible reason 2: I used the Python unit-test method to run, and later changed to Python Run to generate the report normally after running

7) Add the code fp.close() to run

By needs to be imported in advance

from selenium.webdriver.common.by import By

错误“AttributeError:’builtin_function_or_method’ object has no attribute ‘sleep’”

Python programming, I encountered this error when I used time.sleep(n) to suspend the function

Reason: The use of import is from time import *

Solution: replace the import with import time

Reference documents:

Python error - error "AttributeError: 'builtin_function_or_method' object has no attribute 'sleep'" - Xuelan Lingying's Blog - CSDN Blog

8) Rerun 

Reference Document: Several Methods to Solve NoSuchElementException: Unable to locate element (NoSuchElementException: Unable to locate element)

The analysis may be an error caused by insufficient loading time, because the browser has been closed when the assertion is executed

 time.sleep(10): If you extend the waiting time, will you skip to Tencent courses?

 time.sleep(3) will report a bug

No assertions, no errors

Try making it longer

Error:

selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 92
Current browser version is 101.0.4951.67 

The reason is: the version of the browser is inconsistent, it should be that the browser has been automatically updated

9) python reported TypeError: 'str' object is not callable error solution

Reference documents:

Python reported TypeError: 'str' object is not callable error solution - python - web tutorial network

Find out why:

Example 3: Function or string call error

def get_list_urls(self):

for list in self.domainList:

#print(self.domainList[list])

url = self.domainList[list]

reqs = requests.get(url = url, headers = self.headers)

html = reqs.text()

bfHtml = BeautifulSoup(html)

The above example will also report: 'str' object is not callable

The reason is: the return value reqs of requests does not have a text() method, BeautifulSoup receives a string, and we mistakenly wrote reqs.text as reqs.text(), which caused this error.

Modified code below: 

10) Completely modified code:

import unittest
import time
from selenium import webdriver
from selenium.webdriver.common.by import By

#必须要继承unittest.TestCase
class TestCase(unittest.TestCase):

#setUP用例执行前要执行的一个方法
def setUp(self)->None:
#登录的准备工作:打开浏览器,访问登录页
#通过浏览器驱动打开谷歌浏览器
self.driver=webdriver.Chrome()
#访问登录页
self.driver.get('http://39.98.138.157/shopxo/index.php?s=/index/user/logininfo.html')

#tearDown用例执行之后要执行的一个方法
def tearDown(self)->None:
#关闭浏览器,一般会等待三秒钟关闭浏览器
#alt+enter自动导包
time.sleep(4)
self.driver.quit()

#测试过程测试方法一条测试用例就是一条test一个方法必须以test开头
#用户名正确。密码不正确
def test_01(self):
#找到输入框,输入用户名
#找到输入框,输入密码
#找到登录按钮并点击登录
self.driver.find_element(By.XPATH,'/html/body/div[4]/div/div[2]/div[2]/form/div[1]/input').send_keys('666666')
self.driver.find_element(By.XPATH,'/html/body/div[4]/div/div[2]/div[2]/form/div[2]/input').send_keys('633333')
self.driver.find_element(By.XPATH,'/html/body/div[4]/div/div[2]/div[2]/form/div[3]/button').click()
#预期结果
#expected='密码错误'
#做断言
#一般而言使用ifexpected==
#unittest提供了断言
time.sleep(1)
msg=self.driver.find_element(By.XPATH,'//*[@id="common-prompt"]/p').text
self.assertEqual('密码错误',msg)

if__name__=='__main__':
unittest.main()

Note: time.sleep(1) cannot be omitted

It can be optimized later, so that he can wait in time

11) Running error report

AttributeError: 'str' object has no attribute 'decode' solution

Reference documents:

AttributeError: 'str' object has no attribute 'decode' solution_Lemon is not my cute blog-CSDN blog

 The reason for the analysis is the difference between pyhon2 and python3

12) After the modification is completed

 13) Execution

1. unittest comes with a test report

2. The test report generated by htmlTextRunner.py is html code when opened by pycharm

 

 3. Continue to optimize the test report

Reference documents:

Python3 Date and Time | Novice Tutorial

 The test report is generated in the wrong place and the path is wrong

This way the path is where I want it

Full code:

import os
import time
import unittest
import HTMLTestRunner

#找到用例defaultTestLoader默认加载
#discover发现,找
#discover(self,start_dir目录,pattern='test*.py'用例文件,top_level_dir=None):
base_dir=os.path.dirname(os.getcwd())

#dict_dir='testCase20211214'
#unittest*.py执行目录下面以unittest开头的文件都可以执行
#找到用例
discover=unittest.defaultTestLoader.discover(base_dir,'unittest*.py')
#将测试报告转成文本的方式
#withopen('./report.txt','w')asf:
##执行用例TextTestRunner以文本形式展示用例run运行可以批量执行用例stream=f写入verbosity012测试报告的详细程度2表示详细
#unittest.TextTestRunner(stream=f,verbosity=2).run(discover)
#二进制wb
#withopen('./report1.html','wb')asf:
#HTMLTestRunner.HTMLTestRunner(stream=f,verbosity=2).run(discover)

#优化用时间命名测试报告测试报告生成时间+后缀码2021-11-1621-10-20test_report.html
now=time.strftime('%Y-%m-%d-%H-%M-%S')
report_name='./report'+'/'+now+'test_report.html'
withopen(report_name,'wb')asf:
HTMLTestRunner.HTMLTestRunner(stream=f,verbosity=2).run(discover)
f.close()

 

The focus of optimization is: a test report will be generated every time it is executed, and each test report has an execution time, so that it will not be like the previous two cases, and the test report that appears in the subsequent execution will cover the previous test report

 4. Continue to optimize and expand:

1. The test report generated by HTMLtextrunnerCN.py is better than HtmlTestRunner

2、BSTestRunner.py

3. Allure test report platform

The difference between test reports is whether they look good or not, and which ones look better. Showing them to leaders and colleagues makes them feel cool.

The direction of personal expansion: for example

The use case execution is wrong, add a screenshot

3. Knowledge points

find_element_by_xpath cannot be used

solution:

find_element_by_* has been deprecated use find_element instead

Reference documents:

http://blog.sina.com.cn/s/blog_53a99cf301030fv4.html

original wording

browser.find_element_by_class_name("sort-area")

browser.find_element_by_xpath("//*[text()='time sorting']")

current way of writing

browser.find_element(By.CLASS_NAME,"sort-area")

browser.find_element(By.XPATH,"//*[text()='time sorting']")

source code:

使用find_element代替

def find_element(self,by=By.ID,value=None)->WebElement:
"""
FindanelementgivenaBystrategyandlocator.

:Usage:
::

element=driver.find_element(By.ID,'foo')

:rtype:WebElement
"""
if isinstance(by,RelativeBy):
return self.find_elements(by=by,value=value)[0]

if by==By.ID:
by=By.CSS_SELECTOR
value='[id="%s"]'%value
elif by==By.TAG_NAME:
by=By.CSS_SELECTOR
elif by==By.CLASS_NAME:
by=By.CSS_SELECTOR
value=".%s"%value
elif by==By.NAME:
by=By.CSS_SELECTOR
value='[name="%s"]'%value

return self.execute(Command.FIND_ELEMENT,{
'using':by,
'value':value})['value']

Guess you like

Origin blog.csdn.net/Lynn1111111/article/details/124861058