UnitTest案例

#!/usr/bin/env python

# -*- coding: utf-8 -*-

# @Time    : 2019/3/15 20:29


import unittest

import HTMLTestRunner

import time

from seleniumimport webdriver

#导入测试用例文件

class UI_Test(unittest.TestCase):

def setUp(self):

self.driver = webdriver.Chrome

self.driver.maximize_window()

self.driver.implicitly_wait(5)

self.base_url ="https://www.baidu.com/"

    def tearDown(self):

self.driver.quit()

def Test1(self):

driver =self.driver

driver.get(self.base_url)

driver.find_element_by_xpath("//*[@id='kw']").send_keys("python+selenium+unittest")

driver.find_element_by_xpath("//*[@id='su']").click()

time.sleep(5)

def create_suit():

# 创建测试用例集

        testunit = unittest.TestSuite()

# 判断是否为测试用例,自动加载测试用例到测试套件中

        cslist ="C:\\Users\\13928\\Desktop\\UI_Test"

    # discover方法定义

        discover = unittest.defaultTestLoader.discover(

# 测试用例放置的文件夹名

            cslist,

            pattern='test_*.py',

            top_level_dir=None

        )

# discover方法筛选出来的用例,循环添加到测试套件中

        for test_suitein discover:

testunit.addTests(test_suite)

print(testunit)

return testunit

alltestnames = createsuit()

if __name__ =="__main__":

now = time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime(time.time()))

# 定义报告存放路径

    filename ='C:\\Users\\13928\\Desktop\\UI_Test' + now +'result.html'

    fp =open(filename, 'wb')

# 定义测试报告

    runner = HTMLTestRunner.HTMLTestRunner(

stream=fp,

        title=u'我的测试报告',

        description=u'用例执行情况:'

    )

# 执行测试

    runner.run(alltestnames)

fp.close()

你可能感兴趣的:(python)