简体   繁体   English

如何在Python单元测试中创建HTML报告?

[英]How to create HTML reporting in Python unit test?

I'm automating mobile app with Appium and Python. 我正在使用Appium和Python自动化移动应用程序。 I need to get a HTML report for the test results. 我需要获取测试结果的HTML报告。 What I need is to save test results in Html files, for human readable presentation of results. 我需要将测试结果保存在Html文件中,以便于人类可读地呈现结果。 I have tried out a few ways, but nothing worked for me. 我尝试了几种方法,但是没有任何效果。

Anyone knows how to do it? 有人知道该怎么做吗? Thanks in advance. 提前致谢。

import os, sys
import glob
import unittest
from appium import webdriver
from time import sleep

PLATFORM_VERSION = '5.1.1'


class EntranceTests(unittest.TestCase):

def setUp(self):
    print 'commandline args',sys.argv[1]
    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '5.1.1'
    desired_caps['deviceName'] = 'CooTel S32'   
    desired_caps['udid'] = sys.argv[1] 
    desired_caps['appPackage'] = 'com.android.systemui'
    desired_caps['appActivity'] = ' '
    url = "http://localhost:{}/wd/hub".format(sys.argv[2])
    self.driver = webdriver.Remote(url, desired_caps)


def data_connection(self):
    self.driver.orientation = "PORTRAIT"
    self.driver.swipe(340, 1, 340, 800, 2000)

    notification = self.driver.find_element_by_id('com.android.systemui:id/header')
    notification.click()

    try:
            wifi = self.driver.find_element_by_xpath('//*[contains(@class,"android.widget.TextView") and contains(@text, "WLAN")]')
            wifi.is_displayed()
            print 'Wifi is switched off'

            mobiledata = self.driver.find_element_by_xpath('//android.widget.TextView[contains(@text, "Mobile data")]')
            mobiledata.click()
            print 'SUCCESS! Switch on Mobile data'
            sleep(5)

    except:
            print 'Wifi is switched on'

            wifi_off = self.driver.find_element_by_xpath('//*[contains(@class,"android.widget.ImageView") and contains(@index, "0")]')
            wifi_off.click()
            print 'SUCCESS! Switch off Wifi'

            mobiledata = self.driver.find_element_by_xpath('//android.widget.TextView[contains(@text, "Mobile data")]')
            mobiledata.click()
            print 'SUCCESS! Switch on Mobile data'
            sleep(5)


def testcase_dataAndWifi(self):
    self.data_connection()


def tearDown(self):
    self.driver.quit()


if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(EntranceTests)
result = str(unittest.TextTestRunner(verbosity=2).run(suite))

You can use nose-html-reporting module(pip install nose-html-reporting) for generating HTML reports while using python unittest framework. 您可以在使用python unittest框架的同时使用鼻子-html-报告模块(pip install鼻子-html-报告)来生成HTML报告。

Please refer below link for more information: https://pypi.org/project/nose-html-reporting/ 请参考以下链接以获取更多信息: https : //pypi.org/project/nose-html-reporting/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM