Python:没有这样的测试方法:如何通过从另一个方法显式调用来执行测试方法?
这是我的Python:没有这样的测试方法:如何通过从另一个方法显式调用来执行测试方法?,python,flask,Python,Flask,这是我的LoginResourceHelper测试类 from flask.ext.testing import TestCase class LoginResourceHelper(TestCase): content_type = 'application/x-www-form-urlencoded' def test_create_and_login_user(self, email, password): user = U
LoginResourceHelper
测试类
from flask.ext.testing import TestCase
class LoginResourceHelper(TestCase):
content_type = 'application/x-www-form-urlencoded'
def test_create_and_login_user(self, email, password):
user = UserHelper.add_user(email, password)
self.assertIsNotNone(user)
response = self.client.post('/', content_type=self.content_type,
data=UserResourceHelper.get_user_json(
email, password))
self.assert200(response)
# HTTP 200 OK means the client is authenticated and cookie
# USER_TOKEN has been set
return user
def create_and_login_user(email, password='password'):
"""
Helper method, also to abstract the way create and login works.
Benefit? The guts can be changed in future without breaking the clients
that use this method
"""
return LoginResourceHelper().test_create_and_login_user(email, password)
当我调用create\u和\u login\u user('test\u get\u user')
时,我看到如下错误
line 29, in create_and_login_user
return LoginResourceHelper().test_create_and_login_user(email, password)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 191, in __init__
(self.__class__, methodName))
ValueError: no such test method in <class 'core.expense.tests.harness.LoginResourceHelper.LoginResourceHelper'>: runTest
第29行,在创建用户和登录用户中
返回LoginResourceHelper()。测试\u创建\u和\u登录\u用户(电子邮件、密码)
文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py”,第191行,在__
(self.\uuuuu类\uuuuuu,方法名))
ValueError:在:runTest中没有此类测试方法
Python的unittest
模块(Flask在幕后使用)以特殊的方式组织代码
为了从派生自TestCase
的类运行特定方法,您需要执行以下操作:
LoginResourceHelper('test_create_and_login_user').test_create_and_login_user(email, password)
class ExampleTestcase(TestCase):
def test_foo(self):
# Do assertions here
def test_bar(self):
# Do other assertions here
untitest在幕后做什么 为了理解为什么必须这样做,您需要了解默认的
TestCase
对象是如何工作的
通常,当继承时,TestCase
希望有一个runTest
方法:
class ExampleTestCase(TestCase):
def runTest(self):
# Do assertions here
但是,如果您需要有多个测试用例
,那么您需要为每个测试用例都这样做
由于这是一件乏味的事情,他们决定:
LoginResourceHelper('test_create_and_login_user').test_create_and_login_user(email, password)
class ExampleTestcase(TestCase):
def test_foo(self):
# Do assertions here
def test_bar(self):
# Do other assertions here
这称为测试夹具。但是因为我们没有声明一个runTest()
,所以现在必须指定希望TestCase运行的方法—这就是您想要做的
>>ExampleTestCase('test_foo').test_foo()
>>ExampleTestCase('test_bar').test_bar()
通常情况下,unittest
模块将在后端完成所有这些操作,以及其他一些操作:
- 将测试用例添加到测试套件(通常使用TestLoader完成)
- 调用正确的TestRunner(它将运行所有测试并报告结果)
unittest
执行,因此必须定期执行unittest
所做的工作
为了深入理解,我强烈建议您阅读文档。将
LoginResourceHelper()
更改为LoginResourceHelper
TypeError:unbound方法测试\u创建\u和\u登录\u用户()必须使用LoginResourceHelper实例作为第一个参数进行调用,当我这样做时,我认为您需要首先实例化该类,因为调用函数在该类之外。类似于self.\u loginHelper=LoginResourceHelper
然后将返回更改为return self.\u loginHelper.test\u create\u和\u login\u user(电子邮件、密码)