Appium+python Entry code for mobile testing (iv)

Source: Internet
Author: User
Tags appium

The idea of automating regression testing in recent work is to automate each test case, then compare the last stable version with the two versions of the current test, and put two versions in the test report to facilitate manual verification. The initial idea was to generate a test report with Htmltestrunner, but the htmltestrunner itself was not functional and it did not support Python 3, so another tool was found to do it.

Introduction to generating test report tools

Htmltestrunner is a tool that saves the results of a unit test as a test report in HTML format, and the functionality and Htmltestrunner of this tool are very much like the name. But it supports PYTHON3, and the resulting report style is more aesthetically pleasing.

Installing Htmltestrunner

To access this URL:
https://pypi.python.org/pypi/html-testRunner/1.0.3
Then download the HTML_TESTRUNNER-1.0.3-PY2.PY3-NONE-ANY.WHL file and then install it using the following command (for Linux systems like Ubuntu):

$ sudo pip3 install html_testRunner-1.0.3-py2.py3-none-any.whl

If you are a Windows system, try to install it using the following command:

$ pip install html_testRunner-1.0.3-py2.py3-none-any.whl
Add Features

Htmltestrunner this tool, which itself does not show functionality, but can be modified by modifying its template file to add functionality.
Download the Htmltestrunner source code to local via the address below:

Https://github.com/oldani/HtmlTestRunner

Open the htmltestrunner/template/report_template.html file with the editor and modify it:

You need to remove the red code and add the green code. Note Set the new picture's directory to your local saved directory.

Generate Automated test reports

Using the Python Appium API and the UnitTest Unit test framework, together with the Htmltestrunner third-party modules mentioned above, you can generate test reports with tests. The specific code is as follows:

#!/usr/bin/env Python3# Coding=utf-8FromAppiumImportWebdriverFromNose.toolsImport*ImportUnitTestImportHtmltestrunner#生成HTML格式的测试报告ClassTake_screen_shot():#这个类将在下面作为装饰器使用Def__init__(Self,Func):Self.Func=FuncSelf.Name=Func.__name__+' (__main__. caltestcase). png '#拼接文件名Def__call__(Self,*Args):#对每次调用的函数都做操作Try:Self.Func(Self,*Args)Finally:Driver.Get_screenshot_as_file(Self.Name)ClassCaltestcase(UnitTest.TestCase):@classmethodDefSetupClass(Self):Desired_caps={}Desired_caps[' PlatformName ']=' Android '#设置操作平台Desired_caps[' Platformversion ']=' 5.1.1 '#操作系统版本Desired_caps[' DeviceName ']=' Nexus 4 '#设备名称Desired_caps[' Apppackage ']=' Com.android.calculator2 'Desired_caps[' Appactivity ']=‘. Calculator 'Desired_caps[' Udid ']=' 04c5a5af52197902 'GlobalDriverDriver=Webdriver.Remote(' Http://127.0.0.1:4723/wd/hub ',Desired_caps)@classmethodDefTeardownclass(Self):Driver.Quit()#退出当前应用@take_screen_shot#对每一条测试用例使用装饰器DefTest_case_01(Self):Text=Driver.find_element_by_id(' Com.android.calculator2:id/formula ').TextAssert_equal(Text,"")@take_screen_shotDefTest_case_02(Self):Driver.find_element_by_id(' Com.android.calculator2:id/digit_1 ').Click()#点击数字键1Driver.find_element_by_id(' Com.android.calculator2:id/op_add ').Click()#点击加号Driver.find_element_by_id(' Com.android.calculator2:id/digit_1 ').Click()#点击数字键1Driver.find_element_by_id(' Com.android.calculator2:id/eq ').Click()#点击等号Text=Driver.find_element_by_id( ' Com.android.calculator2:id/formula ' ) . Text assert_equal (text "2" ) if __name__ ==  ' __main__ ' :  #下面语句用来生成测试报告 unittest. Main (testrunner=htmltestrunner . htmltestrunner (output= ' Cal_ Report '  report_title= ' Android Calculator test reports '               /span>                

Code Analysis: Python's adorner is used because each test case is scheduled to operate. This enables the operation of each test case after execution, whether successful or unsuccessful, using the Htmltestrunner tool to generate the test report at the end of the code.

Next, use the following command to perform the test:

$ python3 appium_deom.pyRunning tests... ---------------------------------------------------------------------- test_case_01 (__main__.CalTestCase) ... OK (1.786675)s test_case_02 (__main__.CalTestCase) ... OK (5.081634)s----------------------------------------------------------------------Ran 2 tests in 0:00:OKGenerating HTML reports...

The location of the generated test report is located in the reports directory under the directory where the test script resides, and is viewed through the browser with the following results:

Click the View button to expand the view and error messages:

The next thing you want to do

Now the test report in the HTML template is still a dead path, if the path can be automatically generated, it is better.
There are now no more than two versions of the comparison, consider continuing to look for tools to do the contrast, and highlight or mark the two pictures are not the same place.

Reprint Source

Appium+python Entry code for mobile testing (iv)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.