码迷,mamicode.com
首页 > 系统相关 > 详细

unittest模块在linux报错: AttributeError: module 'unittest' has no attribute 'TestRunner'

时间:2020-06-20 10:43:27      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:win   Suite   tca   python   att   exce   drive   window   except   

一开始在windows下运行没有问题,但是在linux下运行却报如下错误:

? AttributeError: module ‘unittest‘ has no attribute ‘TestRunner‘,

在网上找了好多资料看了一下都提示存在与unittest模块同名的.py文件

但是我查了一点也没有发现与之同名的文件,最后偶然间看到有人用TextTestRunner()这个方法于是把TestRunner换成TextTestRunner问题解决下面是我的代码:

import unittest
from selenium import webdriver
import time


class Search_KeyWords(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(30)
        self.url = "http://www.baidu.com/"

    def test_search_davieyang(self):
        driver = self.driver
        driver.get(self.url)
        driver.find_element_by_id("kw").clear()
        driver.find_element_by_id("kw").send_keys("davieyang")
        driver.find_element_by_id("su").click()
        time.sleep(3)
        try:
            self.assertTrue("davieyang" in driver.page_source)
        except AssertionError as e:
            raise e

    def test_search_selenium(self):
        driver = self.driver
        driver.get(self.url)
        driver.find_element_by_id("kw").clear()
        driver.find_element_by_id("kw").send_keys("selenium")
        driver.find_element_by_id("su").click()
        time.sleep(3)
        try:
            self.assertTrue("davieyang" in driver.page_source)
        except AssertionError as e:
            raise e

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


if __name__ == ‘__main__‘:
    suite = unittest.TestSuite()
    suite.addTest(Search_KeyWords(‘test_search_davieyang‘))
    suite.addTest(Search_KeyWords(‘test_search_selenium‘))
    runner = unittest.TestRunner()
    runner.run(suite)

修改后的代码:

import unittest
from selenium import webdriver
import time


class Search_KeyWords(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(30)
        self.url = "http://www.baidu.com/"

    def test_search_davieyang(self):
        driver = self.driver
        driver.get(self.url)
        driver.find_element_by_id("kw").clear()
        driver.find_element_by_id("kw").send_keys("davieyang")
        driver.find_element_by_id("su").click()
        time.sleep(3)
        try:
            self.assertTrue("davieyang" in driver.page_source)
        except AssertionError as e:
            raise e

    def test_search_selenium(self):
        driver = self.driver
        driver.get(self.url)
        driver.find_element_by_id("kw").clear()
        driver.find_element_by_id("kw").send_keys("selenium")
        driver.find_element_by_id("su").click()
        time.sleep(3)
        try:
            self.assertTrue("davieyang" in driver.page_source)
        except AssertionError as e:
            raise e

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


if __name__ == ‘__main__‘:
    suite = unittest.TestSuite()
    suite.addTest(Search_KeyWords(‘test_search_davieyang‘))
    suite.addTest(Search_KeyWords(‘test_search_selenium‘))
    runner = unittest.TextTestRunner()
    runner.run(suite)

unittest模块在linux报错: AttributeError: module 'unittest' has no attribute 'TestRunner'

标签:win   Suite   tca   python   att   exce   drive   window   except   

原文地址:https://www.cnblogs.com/fangyingdong/p/13167660.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!