I'm finally setting up testing for my Django app, but I'm having difficulties getting started. I'm using model_mommy
to create dynamic data for my tests, but have the following problem:
The view I'm testing is supposed to show me all the assignments
a particular user has to complete. To test this, I want to create 500 assignments, log into the app and check if they are shown. So far I have the following test cases:
class TestLogin(TestCase):
def setUp(self):
self.client = Client()
user = User.objects.create(username='sam')
user.set_password('samspassword')
user.save()
def test_login(self):
self.client.login(username='sam', password='samspassword')
response = self.client.get('/')
print (response.content)
self.assertEqual(response.status_code, 200)
and
class TestShowAssignments(TestCase):
def setUp(self):
user_recipe = Recipe(User, username='sam', password="samspassword")
self.assignment = Recipe(Assignment,
coders = related(user_recipe))
self.assignments = self.assignment.make(_quantity=500)
def test_assignments(self):
self.assertIsInstance(self.assignments[0],Assignment)
self.assertEqual(len(self.assignments),500)
The first test passes fine and does what it should: TestLogin
logs the user in and shows his account page. The trouble starts with TestShowAssignments
, which creates 500 assignments but if I look at the assignments with print (self.assignments[0].coders)
, I get auth.User.None
. So it doesn't add the user I defined as a relation to the assignments. What might be important here is that the coders
field in the model is a m2m field, which I tried to address by using related
, but that doesn't seem to work.
What also doesn't work is logging in: if I use the same code I use for logging in during TestLogin
in TestShowAssignments
, I can't log in and see the user page.
So, my question: How do I use model_mommy
to create Assignments and add them to a specific user, so that I can log in as that user and see if the assignments are displayed properly?
Aucun commentaire:
Enregistrer un commentaire