Can a class which inherits from factory.DjangoModelFactory use Django TestCase asserts?

38 views
Skip to first unread message

אורי

unread,
Jan 12, 2019, 9:03:40 AM1/12/19
to django...@googlegroups.com
Hi,

I have a problem with tests. We defined a class called DefaultUserFactory which inherits from factory.DjangoModelFactory, and I want to use asserts in this class such as assertEqual. What is the best approach to do it? I tried to inherit both from factory.DjangoModelFactory and from Django class TestCase but it doesn't work. I thought about passing a parameter from the calling function and then call assertEqual of the calling function's self. But it seems too complicated. Is there a better approach?

You can see our code on GitHub:



Thanks,
אורי (Uri)

Michal Petrucha

unread,
Jan 14, 2019, 11:48:17 AM1/14/19
to django...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
Hi,

The factory class is not really the place to put your test assertions.
That would be the responsibility of the code of your actual test
cases. Factories are just one of the tools that you happen to mostly
use in tests, but you can totally use them to generate a sample
database outside of an automated test environment.

Running this kind of test code in your factory would also have the
drawback that you'd run the same asserts over and over and over again
every single time you invoke the factory – which might be thousands of
times during one run of your test suite.

As I understand it, you want to test your factories, which is a good
idea. What I suggest you do is to just write a separate test module
for your factories.

Good luck,

Michal
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBCgAGBQJcPL0tAAoJEHA7T/IPM/klKeUQAMYUPpVVXAazNEt6Ga2He5V3
ZmmCDyYqQIJvrDAEwX7S1HbOq2K+EjvKeL2/dAf0Si7SQJPsjHcElUOIuSs+ch+/
46C5KM+3LLf3v2H6u1793TQy+oImoaygv8Mp3OZ8j7bfUH2dsBiLhECmfwQz48Ra
Uw/p7FoKKsT5saVK/bA4uoI2mIrnBKCj2KiZyYbkRs928tP9tAYIsHAotSXHDZVo
at8yyJ+J1nFFAzhZPJWN72zK0Pw5RdPF2L3wOrRhVgHLDfYsaZI9O4+6HYBjKkPn
6YWTVG1xq5p+dfofvtdzzFOiDn2rBRWgLlx3zl9cVE6xY7HjaieCI1a9IaePFXy8
MRIytt1Cc41tyT1lCl2yFBc+NT0+DFZMizNUkTgdzwM2uL2BlIh3+GsEY82ZzX/H
iwZ8DSOVuE2Iqpo132LrqcllHqNehxSYya7WBeBYHJiuDq2b2OaJp6ko5eb13ukn
LaVZGVwl+dmSx9HhVDqYHWHxK7AuEiE0CI+Y7Oy7RQhSH97L2MEw1qQlt6UxZtiz
83U0+RbNjDqXN0oMhswHtbnvnqnvKMg5zHVjzoLdAxrT+IIWUAnP6IqIM9v4UkgX
1aPof7eI6nm//ZA+fjmjCRorF3rHi98ZeK0a9Xccakl09zNKX7Vc/nlIbQqlKV8e
YawH4kZesUSUWBkIuIV/
=0T5r
-----END PGP SIGNATURE-----

אורי

unread,
Jan 16, 2019, 8:06:25 AM1/16/19
to django...@googlegroups.com
Hi Michal,

I test the models and not the factories. The factories in this case are only used for testing, they are not used in production. And I prefer to test the factory every time it is invoked even if it means the tests will take more time to complete.

Anyway, in this case the solution I found is - I created a new class and a new object - `_test_case_with_mixin` - which is called from the factory to assert that the model is working correctly (in this case, assigning the user's first and last name in all languages). And the factory is calling asserts from this object and not directly with self. The code can be seen on https://github.com/speedy-net/speedy-net/blob/staging/speedy/core/accounts/test/base_user_factories.py. In this case, I checked and commenting `def clean_all_fields` in our User model causes the tests to fail, which is what I expect. I only wrote this method a few days ago to fix a bug in the models.

In another factory, I also created another object called `_test_case` to assert other things where the mixin is not needed:

Thanks,
אורי (Uri)


--
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/20190114164741.GV8269%40koniiiik.org.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages