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

Python unittest报错:TestSequenceFunctions未定义的原因排查

Why do I get "NameError: name 'TestSequenceFunctions' is not defined" when running my Python unittest code?

Let's break down exactly what's going on here. First, let's recap your code and the error you're seeing:

Your Code

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(unittest.TestSequenceFunctions)
    unittest.TextTestRunner(verbosity=2).run(suite)

The Error

Traceback (most recent call last):
  File "test_CGSSv00.py", line 109, in <module>
    suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
NameError: name 'TestSequenceFunctions' is not defined

The Root Cause

This error is straightforward: the TestSequenceFunctions class you're trying to use doesn't exist in your script's current namespace. Here's why that's happening:

  • You didn't define the test class locally
    TestSequenceFunctions isn't a built-in class in the unittest module. It's supposed to be a custom test class you write that inherits from unittest.TestCase. If you haven't written this class anywhere in your script, Python has no idea what it refers to.

  • You didn't import the class from another module
    If TestSequenceFunctions is defined in a different .py file (like a separate test module), you need to explicitly import it into your current script—otherwise, Python can't access it.

  • You had a syntax mistake in your code
    Your original code tried to use unittest.TestSequenceFunctions—but again, that class doesn't live inside the unittest module. That's a typo; you should be referencing your own custom class directly (not prefixed with unittest.).


Fixes to Try

Option 1: Define the Test Class in Your Script

Add your custom test class above the if __name__ == '__main__' block:

import unittest

# Define your test class first
class TestSequenceFunctions(unittest.TestCase):
    def test_example(self):
        # Add your test logic here
        self.assertEqual(2 + 2, 4)

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
    unittest.TextTestRunner(verbosity=2).run(suite)

Option 2: Import the Class from Another Module

If TestSequenceFunctions is in a file named my_test_classes.py, import it like this:

import unittest
from my_test_classes import TestSequenceFunctions  # Import the existing class

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
    unittest.TextTestRunner(verbosity=2).run(suite)

内容的提问来源于stack exchange,提问作者Python Jim

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:51:54