flask - Python: no such test method: How can I execute a test method by calling it explicitly from another method? -


here loginresourcehelper test class

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 client authenticated , cookie             # user_token has been set              return user      def create_and_login_user(email, password='password'):         """         helper method, abstract way create , login works.         benefit? guts can changed in future without breaking clients         use method         """         return loginresourcehelper().test_create_and_login_user(email, password) 

when call create_and_login_user('test_get_user'), see error following

 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 

python's unittest module (which flask using behind scenes) organizes code in special way.

in order run specific method class derived of testcase need following:

loginresourcehelper('test_create_and_login_user').test_create_and_login_user(email, password) 

what untitest behind scenes

in order understand why must this, need understand how default testcase object works.

normally, when inherited, testcase expecting have runtest method:

class exampletestcase(testcase):     def runtest(self):        # assertions here 

however if needed have multiple testcases need every single one.

since tedious thing do, decided following:

class exampletestcase(testcase):     def test_foo(self):         # assertions here      def test_bar(self):         # other assertions here 

this called test fixture. since did not declare runtest(), must specify method want testcase run - want do.

>>exampletestcase('test_foo').test_foo() >>exampletestcase('test_bar').test_bar() 

normally, unittest module of on end, along other things:

  • adding testcases test suite (which done using testloader)
  • calling correct testrunner (which run of tests , report results)

but since circumventing normal unittest execution, have work unitest regularly does.

for indepth understanding, highly recommend read docs unittest.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -