I am just getting started with Django, so this may be something stupid, but I am not even sure what to google at this point.
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 view, everything is fine. When I write a test case to use any of the methods inside the Utility class, I get None back:
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 someone tell me why this is working in the view, but not working inside of the test case and does not generate any error messages?
Thanks
Verify whether your unit test comply the followings,
TestClass
must be written in a file name test*.py
TestClass
must have been subclassed from unittest.TestCase
TestClass
should have a setUp
function to create objects(usually done in this way, but objects creation can happen in the test functions as well) in the databaseTestClass
functions should start with test
so as to be identified and run by the ./manage.py
test commandTestClass
may have tearDown
to properly end the unit test case.Test Case Execution process:
When you run ./manage.py test
django sets up a test_your_database_name
and creates all the objects mentioned in the setUp
function(Usually) and starts executing the test functions in the order of placement inside the class and once when all the test functions are executed, finally looks for the tearDown
function executes if any present in the test class and destroys the test database.
It may be because that you might not have invoked objects creation in setUp function or elsewhere in the TestClass
.
Can you kindly post the entire traceback and test file to help you better?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With