DEV Community

Discussion on: Make tests a part of your app

Collapse
hanpari profile image
Pavel Morava

Great article, Nikita. Still, I would appreciate an example for your Django's view function if possible.

Collapse
sobolevn profile image
Nikita Sobolev Author

Thanks! What do you want to see in this example? :)

Collapse
hanpari profile image
Pavel Morava

I was wondering how you would test your view:


@require_http_methods(['GET', 'POST'])
@login_required
@never_cache
def my_view(request: HttpRequest) -> HttpRespose:
    ...

Enter fullscreen mode Exit fullscreen mode

This function relies (except for the first decorator) on side effects if I am not mistaken.

The technique you described later on in the article seems to work properly only with pure functions. This is why your example of Django view function confused me.

Thread Thread
sobolevn profile image
Nikita Sobolev Author

Oh, I see.

The technique you described later on in the article seems to work properly only with pure functions

It reallly depends, because returns expects you to separate pure code from impure. But, when testing laws with IO, it is expected to work with impure code.

This is also the case with deal. It even has its own notation for impure code: deal.readthedocs.io/basic/side-eff...

For both libs you can set your state for impure code before the actual test. So, it should work correctly.

Thread Thread
hanpari profile image
Pavel Morava

The side effects testing (examples with stdin, stdout) is sweet but cannot probably verify whether a user is logged or not. Like I said before, I am not sure if Django is the best candidate for being tested this way. Django testing utilities are better equipped for these tests. The communication with Django views is stateful. This is why I was uncertain how would you test it.

Thread Thread
sobolevn profile image
Nikita Sobolev Author

Take a look at stateful testing in hypothesis and schemathesis:

It might cover some basics on how it can be done!