python自动化测试unittest+selenium+HTMLTestRunner(二)

为了使程序更规范,且方便后期维护,故把程序分为四个模块:case、suite、HTMLTestRunner、methods

case:用例模块;编写测试模块的用例数据

suite:测试套件模块;加载需要测试的具体用例

HTMLTestRunner:测试报告模块;定义生成测试报告

methods:方法模块;编写测试模块的用例方法

------------------------------------------------------------------------------------------------------

case模块:

one.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unittest
import two

class MyTestCase(unittest.TestCase):

    # 初始化工作
    def setUp(self):
        pass

    # 退出清理工作
    def tearDown(self):
        pass

    # 具体的测试用例,一定要以test开头
    def test_Case1(self):
        self.assertMultiLineEqual(two.baidu1(), u'百度一下,你就知道')

    def test_Case2(self):
        self.assertMultiLineEqual(two.baidu2(), u'京公网安备11000002000001号')

suite模块:

three.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
import one

def suite1():
    # 创建测试套件
    suite = unittest.TestSuite()
    # 为套件添加测试用例
    suite.addTest(one.MyTestCase("test_Case1"))
    return suite

def suite2():
    # 创建测试套件
    suite = unittest.TestSuite()
    # 为套件添加测试用例
    suite.addTest(one.MyTestCase("test_Case2"))
    return suite

def all_suite():
    suite = unittest.TestSuite((suite2(), suite1()))
    return suite


HTMLTestRunner模块:

four.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import HTMLTestRunner

def save_file():
    # 确定生成报告的路径
    filePath = "pyResult.html"
    fp = file(filePath, 'wb')
    # 生成报告的Title,描述
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title='Python TestReport', description='This  is Python  Report')
    return runner

methods模块:

two.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
from selenium import webdriver

br = webdriver.Chrome()
br.maximize_window()
br.get('http://www.baidu.com')

def baidu1():
    time.sleep(1)
    title = br.title
    return title

def baidu2():
    time.sleep(1)
    jg = br.find_element_by_id('jgwab').text
    return jg

run.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import four
import three
import unittest

if __name__ == '__main__':
    # 调用定义HTMLTestRunner的方法
    runner = four.save_file()
    # 运行suite所组装的测试用例
    # runner = unittest.TextTestRunner()
    # 调用测试套件方法
    all_suite = three.all_suite()
    # 执行测试套件
    runner.run(all_suite)



你可能感兴趣的:(selenium)