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

Posted 烂人与酒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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'(的主要内容,如果未能解决你的问题,请参考以下文章

selenium + python自动化测试unittest框架学习python导入模块及包知识点

Python模块unittest

UnitTest, Mock整数类型返回NPE(null)报错

python unittest错误模块

python中的单元测试模块unittest

unittest