python测试代码报错:Ran 0 test in 0.00s

时间:2021-09-11 02:18:22

最近在学习selenium时,使用unittest框架进行代码测试时,发现报以下错误:

Ran 0 test in 0.00s

OK

原因:

  • unittest提供了全局的main()方法,使得一个单元测试模块变成可以直接运行的测

试脚本,main()方法使用TestLoader类来搜索所有包含在模块中以“test”命名开

头的测试方法,并自动执行它们 。例:test_search、testsearch

故而,将解决办法记录如下(以下是我测试代码,已正常运行):

#!/user/local/bin/python2.7
# -*- coding:utf-8 -*-
#unittest框架测试
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class PythonOrgSearch(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()

#测试方法需以test开头,否则报错:Ran 0 test in 0.00s
def test_search_in_python(self):
driver = self.driver
driver.get("http://www.python.org")
self.assertIn("Python", driver.title)
elem = driver.find_element_by_name("q")
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert 'NO results found.' not in driver.page_source

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

if __name__=="__main__":
unittest.main()

运行结果:

Ran 1 test in 14.929s

OK