unitest 测试集 实例

-->baidy.py

#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re


class Baidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://www.baidu.com/"
self.verificationErrors = []
self.accept_next_alert = True

#百度搜索用例
def test_baidu_search(self):
'''百度搜索'''

driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_id("kw").send_keys("selenium webdriver")
driver.find_element_by_id("su").click()
time.sleep(2)
driver.close()

#百度设置用例
def test_baidu_set(self):
'''百度设置'''
driver = self.driver

#进入搜索设置页
driver.get(self.base_url + "/gaoji/preferences.html")

#设置每页搜索结果为50 条
m=driver.find_element_by_name("NR") #返回列表
print(m)
m.find_element_by_xpath("//option[@value='50']").click()
time.sleep(2)

#保存设置的信息
driver.find_element_by_xpath("//input[@value='保存设置']").click()
time.sleep(2)
alert = driver.switch_to_alert()
print(alert.text) #打印弹窗内容
alert.accept()

def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors) #断言错误列表是否为空

if __name__ == "__main__":
unittest.main()

 

--->youdao.py

 

#coding = utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re

class Youdao(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://www.youdao.com"
self.verificationErrors = []
self.accept_next_alert = True

#有道搜索用例
def test_youdao_search(self):
'''有道搜索'''

driver = self.driver
driver.get(self.base_url + "/")

driver.find_element_by_id("translateContent").send_keys("selenium")
driver.find_element_by_xpath("/html/body/div[5]/div/form/button").click()
time.sleep(2)

def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)

if __name__=="__main__":
unittest.main()

 

--> all.py

#coding = utf-8
import unittest
import baidu,youdao #这里需要导入测试文件
import HTMLTestRunner
import time

testunit = unittest.TestSuite()
#将测试用例加入到测试容器(套件)中
testunit.addTest(unittest.makeSuite(baidu.Baidu))
testunit.addTest(unittest.makeSuite(youdao.Youdao))

#取前面时间
now = time.strftime("%Y-%m-%M-%H_%M_%S",time.localtime(time.time()))
filename = r"F:\技术提高学习\自动化框架\web自动化\实例\实例6_unittest通过测试套件组织用例\{0}result.html".format(now)
print(filename)
fp = open(filename, 'wb')

runner = HTMLTestRunner.HTMLTestRunner(stream = fp, title = '百度搜索测试报告', description = '测试执行情况')

#执行测试用例
runner.run(testunit)

posted @ 2018-07-22 15:56  yanhuidj  阅读(167)  评论(0编辑  收藏  举报