Unittest方法 -- 以test开头实例
Posted teachertao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unittest方法 -- 以test开头实例相关的知识,希望对你有一定的参考价值。
此篇是以下面的test作为实战的 : Unittest方法 -- 项目实现自动发送邮件
1、test_01
import unittest
from selenium import webdriver
class BaiduLink(unittest.TestCase):
def setUp(self):
self.option = webdriver.ChromeOptions()
self.option.add_argument("headless")
self.driver = webdriver.Chrome(chrome_options=self.option)
self.driver.maximize_window()
self.driver.implicitly_wait(30)
self.driver.get("Http://www.baidu.com")
def tearDown(self):
self.driver.quit()
def test_baidu_news(self):
"""百度首页:点击新闻链接是否可正常跳转"""
self.driver.find_element_by_link_text("新闻").click()
self.assertEqual(self.driver.current_url,"http://news.baidu.com/")
def test_baidu_map(self):
"""百度首页:点击地图链接是否可正常跳转"""
self.driver.find_element_by_link_text("地图").click()
self.assertIn("https://map.baidu.com/",self.driver.current_url)
if __name__ == ‘__main__‘:
unittest.main(verbosity=2)
二、test_02
import unittest
from selenium import webdriver
class BaiduSo(unittest.TestCase):
def setUp(self):
self.option = webdriver.ChromeOptions()
self.option.add_argument("headless")
self.driver = webdriver.Chrome(chrome_options=self.option)
self.driver.maximize_window()
self.driver.implicitly_wait(30)
self.driver.get("Https://www.baidu.com/")
def tearDown(self):
self.driver.quit()
def test_baidu_so_enabled(self):
"""百度首页:百度首页输入框可编辑"""
so = self.driver.find_element_by_id("kw")
"""断言"""
self.assertTrue(so.is_enabled())
def test_baidu_so(self):
"""百度首页:测试百度有搜索功能"""
self.driver.find_element_by_id("kw").send_keys("selenium")
# so = send_key("selenium")
self.driver.find_element_by_id("su").click()
"""断言"""
# self.assertEqual(self.driver.find_element_by_id("kw").text, "selenium")
if __name__ == ‘__main__‘:
unittest.main(verbosity=2)
三、test_03
# coding=utf-8
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
import time
import unittest
class BlogTitle(unittest.TestCase):
def setUp(self):
self.option = webdriver.ChromeOptions()
self.option.add_argument("headless")
self.driver = webdriver.Chrome(chrome_options=self.option)
self.driver.get("https://www.cnblogs.com/Teachertao/")
self.driver.implicitly_wait(10)
def tearDown(self):
self.driver.quit()
def test_blog_01(self):
u"""验证元素title完全等于 Teacher涛 - 博客园"""
# time.sleep(3)
result = EC.title_is(u‘Teacher涛 - 博客园‘)(self.driver)
# print(result())
self.assertTrue(result)
def test_blog_02(self):
u"""验证元素判断title包含Teacher"""
# time.sleep(3)
title = EC.title_contains("Teacher")(self.driver)
# print(title())
self.assertTrue(title)
if __name__ == "__main__":
unittest.main(verbosity=2)
四、test_04
from selenium import webdriver
import unittest
import time
class Blog(unittest.TestCase):
def setUp(self):
‘‘‘登录博客‘‘‘
self.option = webdriver.ChromeOptions()
self.option.add_argument("headless")
self.driver = webdriver.Chrome(chrome_options=self.option)
self.driver.get("https://passport.cnblogs.com/user/signin")
self.driver.implicitly_wait(10)
def login(self,username,passwd):
‘‘‘这里写了一个登录的方法,账号和密码参数化‘‘‘
self.driver.find_element_by_id("LoginName").send_keys(username)
self.driver.find_element_by_id("Password").send_keys(passwd)
self.driver.find_element_by_id("IsRemember").click()
self.driver.find_element_by_id("submitBtn").click()
time.sleep(3)
def is_login_sucess(self):
‘‘‘判断是否获取到登录账户名称‘‘‘
try:
text = self.driver.find_element("id","header_user_left").text
print(text)
return True
except:
return False
def test_login(self):
‘‘‘登录案例参考:账号,密码自己设置‘‘‘
self.login("xxx","xxx")
# 判断结果
result = self.is_login_sucess()
self.assertTrue(result)
def test_02(self):
u‘‘‘登录案例参考:账号,密码自己设置‘‘‘
self.login(u"xxxx", u"xxxx") # 调用登录方法
# 获取登录后的账号名称
text = self.driver.find_element_by_id("lnk_current_user").text
print(text)
# 断言实际结果与期望结果一致
self.assertEqual(text, u"xxx")
def tearDown(self):
self.driver.quit()
if __name__ == ‘__main__‘:
unittest.main()
五、test_05
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
import unittest
class BolgHome(unittest.TestCase):
u‘‘‘博客首页‘‘‘
@classmethod
def setUpClass(cls):
cls.option = webdriver.ChromeOptions()
cls.option.add_argument("headless")
cls.driver = webdriver.Chrome(chrome_options=cls.option)
url = "https://www.cnblogs.com/Teachertao/"
cls.driver.get(url)
cls.driver.implicitly_wait(30)
@classmethod
def tearDownClass(cls):
cls.driver.quit()
def test_01(self):
u‘‘‘验证元素存在:博客园‘‘‘
locator = ("id", "blog_nav_sitehome")
text = u"博客园"
result = EC.text_to_be_present_in_element(locator, text)(self.driver)
self.assertTrue(result)
def test_02(self):
u‘‘‘验证元素存在:首页‘‘‘
locator = ("id", "blog_nav_myhome")
text = u"首页"
result = EC.text_to_be_present_in_element(locator, text)(self.driver)
self.assertTrue(result)
def test_03(self):
u‘‘‘验证元素存在:发新随笔‘‘‘
locator = ("id","blog_nav_newpost")
text = u"发新随笔"
result = EC.text_to_be_present_in_element(locator,text)(self.driver)
self.assertTrue(result)
if __name__ == "__main__":
unittest.main()
以上是关于Unittest方法 -- 以test开头实例的主要内容,如果未能解决你的问题,请参考以下文章
unittest执行顺序,使用unittest.main()按照test开头,由0-9,A-Z,a-z的顺序执行; 可使用TestSuite类的addTest方法改变执行顺序;