Django Test Drive Cannot Run Method

I'm just getting started with Django, so it might be a bit silly, but I'm not even sure what Google is doing at the moment.

I have a method that looks like this:

def get_user(self,user):
    return Utilities.get_userprofile(user)

      

The method looks like this:

@staticmethod
def get_userprofile(user):
    return UserProfile.objects.filter(user_auth__username=user)[0]

      

When I include this in the performance, everything is fine. When I write a test case to use any of the methods inside the Utility class, I return None:

Two test cases:

def test_stack_overflow(self):
    a = ObjName()
    print(a.get_user('admin'))

def test_Utility(self):
    print(Utilities.get_user('admin'))

      

Results:

Creating test database for alias 'default'...
None
..None
.
----------------------------------------------------------------------

      

Can anyone tell me why this works in the view, but doesn't work inside the test case, and doesn't generate any error messages?

thank

+3


source to share


1 answer


Check if your unit test matches the following:

  • TestClass

    must be written in the filename test*.py

  • TestClass

    should be subclassed from unittest.TestCase
  • TestClass

    must have a function setUp

    to create objects (usually done this way, but object creation can also be done in test functions) in a database
  • TestClass

    functions must start with test

    to be identified and executed by the ./manage.py

    test command
  • TestClass

    may have a tearDown

    unit test case to complete correctly.

Test case execution process:



At startup, ./manage.py test

django sets test_your_database_name

and creates all the objects mentioned in the function setUp

(usually) and starts executing the test functions in the order they were placed inside the class and once when all testing functions are executed, at the end it looks for a function tearDown

if it is present in the test class and destroys the test database.

This could be because you may not have called the creation of objects in the setUp function or elsewhere in TestClass

.

Can you kindly post the whole trace and test file to help you better?

+4


source







All Articles