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:
https://github.com/speedy-net/speedy-net/blob/staging/speedy/match/accounts/test/user_factories.py

Thanks,
אורי (Uri)
u...@speedy.net


On Mon, Jan 14, 2019 at 6:47 PM Michal Petrucha <
michal.petru...@koniiiik.org> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA512
>
> On Sat, Jan 12, 2019 at 03:59:59PM +0200, אורי wrote:
> > 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:
> >
> > master:
> >
> https://github.com/speedy-net/speedy-net/blob/master/speedy/core/accounts/tests/test_factories.py
> >
> > staging (under development):
> >
> https://github.com/speedy-net/speedy-net/blob/staging/speedy/core/accounts/tests/test_factories.py
>
> 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-----
>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@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.
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/CABD5YeHWu3pdT0Y9y%2B4OGWGuW2VaefhFv3maXUq8R4J5K5EH3w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to