如何创建unittest类的实例?

2024-10-02 22:27:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个unittest TestCase的实例,以便使用该类的变量和方法,但它给我带来了一个错误:

In [1]: import tests

In [2]: obj = tests.TestDailyAggregations()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-6a5e2091c8a1> in <module>()
----> 1 obj = tests.TestDailyAggregations()

/usr/lib/python2.7/unittest/case.py in __init__(self, methodName)
    187         except AttributeError:
    188             raise ValueError("no such test method in %s: %s" %
--> 189                   (self.__class__, methodName))
    190         self._testMethodDoc = testMethod.__doc__
    191         self._cleanups = []

ValueError: no such test method in <class 'tests.TestDailyAggregations'>: runTest

以下是我的测试.py我要在其中生成TestDailyAggregations实例并在TestWeeklyAggregations中使用其变量user_daily_dict的文件:

^{pr2}$

我该怎么做才能成为实例?在


Tags: 实例noinpytestselfobjtests
1条回答
网友
1楼 · 发布于 2024-10-02 22:27:43

您只需通过向它传递包含的methodName(例如,setUp)来实例化它。下面是TestCase类的代码,如您所见:

def __init__(self, methodName='runTest'):
    """Create an instance of the class that will use the named test
       method when executed. Raises a ValueError if the instance does
       not have a method with the specified name.
    """

实例化后,您可以访问它包含的方法。 (我经常用这个技巧调试我的unittest…)

相关问题 更多 >