首次运行PyCharm ChromeDriver测试求助:unittest执行0个测试用例
Hey there! Let's dig into why your unittest suite is reporting "Ran 0 tests in 0.000s OK" even though you've got Python 2.7.10, Selenium 3.5.9, and PyCharm all configured. Here are the most common culprits and fixes to check:
Your test class doesn't inherit from
unittest.TestCaseThe unittest framework only recognizes classes that subclass
unittest.TestCaseas test classes. If your class is missing this inheritance, the runner won't pick it up. Double-check your class definition:import unittest from selenium import webdriver # Correct: inherits from unittest.TestCase class CompanySiteTests(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() # Or your preferred browser driver # Test methods go hereYour test methods don't start with
test_Unittest only runs methods whose names start with
test_— this is a strict naming convention. If your method names don't follow this, they'll be ignored:# This will be recognized as a test def test_login_flow(self): self.driver.get("https://your-company-site.com/login") # ... test logic here # This WON'T be recognized (wrong prefix) def login_test(self): # ... logic hereYou're not invoking the test runner
If you don't tell unittest to run your tests, it won't! Add this block at the very bottom of your test file to trigger the runner when you execute the script:
if __name__ == '__main__': unittest.main()PyCharm run configuration glitches
Sometimes PyCharm doesn't auto-detect tests properly. Try these quick fixes:
- Right-click your test file in the project explorer and select Run 'Unittest in [your-file-name.py]' — this forces PyCharm to use the correct runner for that file.
- Go to Run > Edit Configurations and verify the configuration is set to "Unittest", pointing to your test file or class, and using your configured Python 2.7.10 interpreter.
- Refresh your project (right-click the project root > Refresh) to make sure PyCharm picks up any changes to your test structure.
If you share a snippet of your actual test code, we can narrow this down even further!
内容的提问来源于stack exchange,提问作者dsmith

