Python搭配unittest

Posted 此生不换Yang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python搭配unittest相关的知识,希望对你有一定的参考价值。

unittest是Python的单元测试框架,

类似于Java里面的TestNG。

 

from time import sleep
from selenium import webdriver
import unittest


class Unittest(unittest.TestCase):
# Unittest类继承unittest.TestCase类

def setUp(self):
# setUp用于设置初始化工作,在每一个测试用例前先被执行
self.driver = webdriver.Firefox()
self.base_url = "https://www.baidu.com/"
self.driver.maximize_window()
sleep(2)

def test_baidu(self):
# 搜索中国的测试用例
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_xpath(".//*[@id=‘kw‘]").send_keys("中国")
driver.find_element_by_xpath(".//*[@id=‘su‘]").click()
sleep(2)
self.assertEqual(driver.find_element_by_xpath(".//*[@id=‘1‘]/h3/a").text, "中国_百度百科")
# 断言中国_百度百科元素

def tearDown(self):
# tearDown方法在每个测试方法执行后调用,用于测试后的清理工作
self.driver.quit()

if __name__ == "__main__":
unittest.main()
# 整个测试过程集成在unitest.main()模块中,其默认执行以test开头的方法































以上是关于Python搭配unittest的主要内容,如果未能解决你的问题,请参考以下文章

python unittest

python——unittest(单元测试)

Python Unittest Discover即使失败也返回退出代码0

Python接口测试之unittest框架

Python unittest模块心得

Python接口测试之unittest