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

一开始在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'

上一篇:Linux_Centos7-----可以通过127.0.0.1和localhost访问tomcat,不能通过ip访问tomcat 的解决方法


下一篇:Linux系统根目录结构(目录含义)