调用test case集,并生成测试报告

结构是

python_unit结构

test_all.py  进行配置,执行所有测试用例集,并合并测试报告到同一个文件
#test_all.py  进行配置,执行所有测试用例集
# coding = utf-8

from time import sleep
from selenium import webdriver
from test01 import project as T1
from test02 import project as T2
#from test.test222 import projectClass
from selenium.webdriver.support.ui import WebDriverWait  
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC  
import unittest,time,os,logging,HTMLTestRunner,sys

#class ptestlist(unittest.TestSuite):
class ptestlist(unittest.TestSuite):
    def test_module():
        suite1 = T1.suite
        suite2 = T2.suite
        alltests = unittest.TestSuite((suite1, suite2))
        return alltests

    suite = test_module()

    filetime=time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
    filetestname=sys.argv[0][sys.argv[0].rfind(os.sep)+1:].split('.py')[0]
    filename=os.path.join(os.getcwd()+'/log/'+filetime+'-'+filetestname+'.html')
    f=open(filename,encoding='utf-8',mode='a')

    runner = HTMLTestRunner.HTMLTestRunner(
        stream=f,
        #title='Test Result/测试结果',
        title=filetestname+'--测试结果--',
        description=filetestname+'--测试报告--',
        verbosity=2)
    runner.run(suite)
test01.py  测试用例集1,也可以单独进行测试并生成测试报告
# test01.py  测试用例集1,也可以单独进行测试并生成测试报告
# coding = utf-8

from time import sleep
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait  
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC  
import unittest,time,os,HTMLTestRunner,sys

class projectTestSuite(unittest.TestSuite):
    def __init__(self):
    # 初始化,添加__init__以供调用,另在此添加所想测试的case形成测试集提供给suite
        unittest.TestSuite.__init__(self,map(projectClass,("test01OK","test02Search")))
    # 如果用于测试的类中所有的测试方法都以test开,可以用PyUnit模块提供的makeSuite()方法来构造一个TestSuite
    # return unittest.makeSuite(projectClass, "test")

class projectClass(unittest.TestCase):
    def setUp(self):
        "just for setUp"
        print("start test projectClass")
        self.driver= webdriver.Chrome()
        self.driver.get("http://www.baidu.com")

    def test01OK(self):
        "test01:测试登录"
        driver = self.driver
        driver.find_element_by_link_text("登录").click()
        driver.find_element_by_id("TANGRAM__PSP_8__userName").send_keys("小泠哦")
        driver.find_element_by_id("TANGRAM__PSP_8__password").send_keys("900617poppyp")
        driver.find_element_by_id("TANGRAM__PSP_8__submit").click()
        sleep(3)
        a = driver.find_element_by_name("tj_home").get_attribute("text")
        if a == "小泠哦":
            print("the admin is 小泠哦")
        else:print("the admin is false")
        sleep(3)
        driver.find_element_by_name("tj_logout").click()
        b = driver.find_element_by_name("lb").get_attribute("text")
        if b == "登录":
            print("It's exist")
        else:print("No exist")

    def test02Search(self):
        "test02:测试搜索"
        driver = self.driver
        driver.find_element_by_id("kw1").send_keys("ETAH")
        driver.find_element_by_id("su1").click()
        sleep(3)
        link1 = driver.find_element_by_id("1")
        linktxt = link1.find_element_by_tag_name("a").get_attribute("text")
        print(linktxt)

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

class project():
    def suite():
        return projectTestSuite()
    suite = suite()

if __name__ == "__main__":
    filetime=time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
    filetestname=sys.argv[0][sys.argv[0].rfind(os.sep)+1:].split('.py')[0]
    filename=os.path.join(os.getcwd()+'/log/'+filetime+'-'+filetestname+'.html')
    #filename='G:/Auto Test/test/log/log.html'
    f=open(filename,encoding='utf-8',mode='a')
    
    runner = HTMLTestRunner.HTMLTestRunner(
        stream=f,
        #title='Test Result/测试结果',
        title=filetestname+'--测试结果--',
        description=filetestname+'--测试报告--',
        verbosity=2)

    runner.run(project.suite)
# test02.py  测试用例集2,也可以单独进行测试并生成测试报告
# coding = utf-8

from time import sleep
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait  
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC  
import unittest,time,os,HTMLTestRunner,sys

class projectTestSuite(unittest.TestSuite):
    def __init__(self):
        unittest.TestSuite.__init__(self,map(projectClass,(
            "test03News",
            #"test04Search",
            "test05Fail"
            )))

class projectClass(unittest.TestCase):
    def setUp(self):
        "just for setUp"
        print("start test projectClass")
        self.driver= webdriver.Chrome()
        self.driver.get("http://www.baidu.com")

    def test03News(self):
        "test03:测试新闻"
        driver = self.driver
        driver.find_element_by_link_text("新 闻").click()
        driver.find_element_by_link_text("互联网").click()
        new = driver.find_element_by_id("body")
        news = new.find_elements_by_tag_name("a")
        for element in news:
            if element.get_attribute("class") =="a3":
                return element.get_attribute("text")

    def test04Search(self):
        "test04:测试搜索"
        driver = self.driver
        driver.find_element_by_id("kw1").send_keys("东信同邦")
        driver.find_element_by_id("su1").click()
        sleep(3)
        link1 = driver.find_element_by_id("1")
        linktxt = link1.find_element_by_tag_name("a").get_attribute("text")
        print(linktxt)

    def test05Fail(self):
        "test05:尝试制造fail"
        driver = self.driver
        A = driver.find_element_by_id("cp").get_attribute("text")
        print("A is:",A)
        self.assertEqual(A, "音乐")

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

class project():
    def suite():
        return projectTestSuite()
    suite = suite()

if __name__ == "__main__":
    filetime=time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
    filetestname=sys.argv[0][sys.argv[0].rfind(os.sep)+1:].split('.py')[0]
    filename=os.path.join(os.getcwd()+'/log/'+filetime+'-'+filetestname+'.html')
    #filename='G:/Auto Test/test/log/log.html'
    f=open(filename,encoding='utf-8',mode='a')

    runner = HTMLTestRunner.HTMLTestRunner(
        stream=f,
        #title='Test Result/测试结果',
        title=filetestname+'--测试结果--',
        description=filetestname+'--测试报告--',
        verbosity=2)

    runner.run(project.suite)
test02.py  测试用例集2,也可以单独进行测试并生成测试报告
# test02.py  测试用例集2,也可以单独进行测试并生成测试报告
# coding = utf-8

from time import sleep
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait  
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC  
import unittest,time,os,HTMLTestRunner,sys

class projectTestSuite(unittest.TestSuite):
    def __init__(self):
        unittest.TestSuite.__init__(self,map(projectClass,(
            "test03News",
            #"test04Search",
            "test05Fail"
            )))

class projectClass(unittest.TestCase):
    def setUp(self):
        "just for setUp"
        print("start test projectClass")
        self.driver= webdriver.Chrome()
        self.driver.get("http://www.baidu.com")

    def test03News(self):
        "test03:测试新闻"
        driver = self.driver
        driver.find_element_by_link_text("新 闻").click()
        driver.find_element_by_link_text("互联网").click()
        new = driver.find_element_by_id("body")
        news = new.find_elements_by_tag_name("a")
        for element in news:
            if element.get_attribute("class") =="a3":
                return element.get_attribute("text")

    def test04Search(self):
        "test04:测试搜索"
        driver = self.driver
        driver.find_element_by_id("kw1").send_keys("东信同邦")
        driver.find_element_by_id("su1").click()
        sleep(3)
        link1 = driver.find_element_by_id("1")
        linktxt = link1.find_element_by_tag_name("a").get_attribute("text")
        print(linktxt)

    def test05Fail(self):
        "test05:尝试制造fail"
        driver = self.driver
        A = driver.find_element_by_id("cp").get_attribute("text")
        print("A is:",A)
        self.assertEqual(A, "音乐")

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

class project():
    def suite():
        return projectTestSuite()
    suite = suite()

if __name__ == "__main__":
    filetime=time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
    filetestname=sys.argv[0][sys.argv[0].rfind(os.sep)+1:].split('.py')[0]
    filename=os.path.join(os.getcwd()+'/log/'+filetime+'-'+filetestname+'.html')
    #filename='G:/Auto Test/test/log/log.html'
    f=open(filename,encoding='utf-8',mode='a')

    runner = HTMLTestRunner.HTMLTestRunner(
        stream=f,
        #title='Test Result/测试结果',
        title=filetestname+'--测试结果--',
        description=filetestname+'--测试报告--',
        verbosity=2)

    runner.run(project.suite)

 




posted @ 2014-04-02 14:58  小泠哦  阅读(731)  评论(0编辑  收藏  举报