testing in django

37 views
Skip to first unread message

Akash Tomar

unread,
May 23, 2017, 3:07:29 AM5/23/17
to Django users
class SignUpTest(TestCase):
def test_createAccount(self):
my_data = {'username': 'akash.tomar107', 'password': 'akashtomar',"email":"akash.t...@gmail.com", "confirm_password":"akashtomar", "type_of_user":"1", "first_name":"akash", "last_name":"tomar"}
response = self.client.post('/signup/', json.dumps(my_data), content_type='application/json')
self.assertIs(json.loads(response.content)["success"], True)

class LoginTest(TestCase):
def test_login(self):
my_data = {'username': 'akash.tomar107', 'password': 'akashtomar'}
response = self.client.post('/login/', json.dumps(my_data), content_type='application/json')
# import pdb; pdb.set_trace()
self.assertIs(json.loads(response.content)["login"], True)


I want to be able to use the data created while running SignUpTest in the LoginTest. How do I achieve that? Please help.

"Александр Христюхин (roboslone)"

unread,
May 23, 2017, 3:51:13 AM5/23/17
to django...@googlegroups.com
Perhaps fixtures is what you want: https://code.djangoproject.com/wiki/Fixtures

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a3625070-4f46-4c68-b912-888b91e42c82%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Antonis Christofides

unread,
May 23, 2017, 4:22:45 AM5/23/17
to django...@googlegroups.com

Hi,

one possibility is this:

class UserTest(TestCase):

    def setUp(self):
        self.my_data = {...}

    def test_create_account(self):
        response = self.client.post('/signup/', json.dumps(self.my_data), content_type='application/json')
        self.assertIs(...)

    def test_login(self):
        ...
In some cases this will work, but it might also not work (e.g. if you want to organize tests differently). I've found Two Scoops' advice invaluable on this issue: 1) Never use fixtures, 2) DRY does not apply to tests. It's fine to repeat the same code again and again when testing.

Regards,

A.
Antonis Christofides
http://djangodeployment.com

Marlysson Silva

unread,
May 29, 2017, 9:54:23 AM5/29/17
to Django users
If you are using the sqlite database to dummy test , you can in a test case class submit the post to sign up form ( and store at database ) and in other class retrieve the user created previously and performs the login .


Em terça-feira, 23 de maio de 2017 04:07:29 UTC-3, Akash Tomar escreveu:
class SignUpTest(TestCase):
def test_createAccount(self):
my_data = {'username': 'akash.tomar107', 'password': 'akashtomar',"email":"akash.toma...@gmail.com", "confirm_password":"akashtomar", "type_of_user":"1", "first_name":"akash", "last_name":"tomar"}
Reply all
Reply to author
Forward
0 new messages