zoukankan      html  css  js  c++  java
  • Python搭配unittest

    unittest是Python的单元测试框架,

    类似于Java里面的TestNG。

    Unittest.py:

    import unittest

    from selenium import webdriver
    from selenium.webdriver.support.wait import WebDriverWait


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

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

    def test_something(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()
    WebDriverWait(driver, 15).until(lambda x: x.find_element_by_xpath(".//*[@id='1']/h3/a"))

    self.assertEqual(driver.find_element_by_xpath(".//*[@id='1']/h3/a").text, "中国_百度百科")
    # 断言中国_百度百科元素

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

    if __name__ == '__main__':
    unittest.main()
    # 整个测试过程集成在unitest.main()模块中,其默认执行以test开头的方法
  • 相关阅读:
    nginx学习1
    win7右下角的网络连接图标不见了~终极必杀技
    centos配置history记录每个用户执行过的命令
    Screen会话命令 Linux
    Linux的运行级别和chkconfig用法
    Linux与Windows中的UTC时间
    Solr 缓存配置
    SolrJ 查询数据
    Solr之困
    solr 查询参数说明
  • 原文地址:https://www.cnblogs.com/yjlch1016/p/8464547.html
Copyright © 2011-2022 走看看