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

Here is my 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 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) 

When I call create_and_login_user('test_get_user') , I see the error as the 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 
+2
source share
1 answer

The Python unittest (which Flask uses behind the scenes) organizes the code in a special way.

To run a specific method from a class derived from TestCase , you need to do the following:

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

What is not suitable for the scene

To understand why you should do this, you need to understand how the default TestCase object works.

Typically, when inherited, TestCase expects the runTest method to be runTest :

 class ExampleTestCase(TestCase): def runTest(self): # Do assertions here 

However, if you need to have several TestCases , you will need to do this for each of them.

Since this is a tedious thing, they decided the following:

 class ExampleTestcase(TestCase): def test_foo(self): # Do assertions here def test_bar(self): # Do other assertions here 

This is called a test fixture. But since we did not declare runTest() , now you must specify which method you want to run TestCase, this is what you want to do.

 >>ExampleTestCase('test_foo').test_foo() >>ExampleTestCase('test_bar').test_bar() 

Typically, the unittest module will do all this on the back, as well as some other things:

  • Adding test packages to a test suite (which is usually done using TestLoader)
  • Calling the correct TestRunner (which will run all the tests and report the results)

But since you unittest normal execution of unittest , you must do the work that unitest does regularly.

For a really deep understanding, I highly recommend you read unittest docs .

+9
source

All Articles