You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium执行异常:Ran 0 tests in 0.000s,编写测试仍无效

解决unittest显示“Ran 0 tests in 0.000s”的问题

你的代码核心问题是类内方法缩进错误,Python是缩进敏感语言,setUp、test_example等方法必须缩进在PythonOrgSearch类的范围内,否则这些方法不属于测试类,unittest无法识别测试用例。

另外注意:ChromeDriver的路径需要指定到具体的可执行文件,而不是文件夹路径,比如r"C:\Users\paulk\Desktop\Selenium\chromedriver.exe"(根据你的ChromeDriver版本调整文件名)。

修正后的代码:

import unittest
from selenium import webdriver
import page # a file that I created

class PythonOrgSearch(unittest.TestCase):
    def setUp(self):
        # 指定ChromeDriver的完整可执行文件路径
        self.driver = webdriver.Chrome(r"C:\Users\paulk\Desktop\Selenium\chromedriver.exe")
        self.driver.get("http://www.python.org")
        self.driver = self.driver

    def test_example(self): # 以test_开头的方法会被unittest识别
        print("Test")
        assert True
    
    def not_a_test(self):
        print("this will not print")
    
    def tearDown(self):
        self.driver.close()

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

运行修正后的代码,unittest就能正确识别并执行test_example测试用例了。

内容的提问来源于stack exchange,提问作者Koushik Paul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 12:22:59