Pytest coverage report 100%

36 views
Skip to first unread message

Salima Begum

unread,
May 18, 2022, 7:21:52 AM5/18/22
to django...@googlegroups.com
Hi all,
How to write a pytest case to get coverage report to 100%.

```
def Account_security(request):
try:
if ((request.session.get('email') is None) or
(request.session.get('email') == "")):
return HttpResponseRedirect("/home")
dict_act = {}
email = request.session.get('email')
# save_url = save_current_url(request, email)
# Based on logged in user email filtering list of objects.
account = Account_history.objects.filter(email=email)
# Calculating logged in time.
for i in account:
some_date = diff_time_Acnt(i)
dict_act[i.id] = some_date
return render(request, 'account_security.html',
{'account': account,

'fullname': fullname(email),
'name': first_last_initial(email),
'time': settings.SESSION_IDLE_TIMEOUT,
'some_date': dict_act,
})
except Exception as e:
logging.error(e)
return render(request, 'account_security.html', {'account':
account, 'fullname': fullname(email),
'name':
first_last_initial(email),
'time':
settings.SESSION_IDLE_TIMEOUT,
'some_date': dict_act,
})
```

How to write a test case for total function without missing any
statement. Please help me out how to write a test case for above
function.

Thanks
~Salima

Kasper Laudrup

unread,
May 18, 2022, 8:55:00 PM5/18/22
to django...@googlegroups.com
On 18/05/2022 09.20, Salima Begum wrote:
> How to write a test case for total function without missing any
> statement. Please help me out how to write a test case for above
> function.
>

You inject values in your test case to make sure all branches are covered.

Sometimes that requires a bit more trickery with mocking out some things
but in the case of the function you've shown it should be fairly simple
to achieve 100% test coverage, at least from the point of a coverage tool.

Any specific parts you're having issues with? I can't help to notice
that you return the exact same thing to the use in the case of any
exception but only log the exception. That seems wrong but I'm not sure
if that's related to your question.

Kind regards,
Kasper Laudrup
OpenPGP_0xE5D9CAC64AAA55EB.asc
OpenPGP_signature

Salima Begum

unread,
May 19, 2022, 3:46:19 AM5/19/22
to django...@googlegroups.com
Hi,
Thank you for your response. We are basically writing a unit test for Account_security function with this unit test code.


Unit test code :
--------------------------------------------------------
    def test_Account_security_url(self):
        path = reverse('account-security')
        assert resolve(path).view_name == 'account-security'
---------------------------------------------------------

Here is the function for which we write above unit test, but the test is not covering these red colored statements. So, we are wondering what could be the issue here.

def Account_security(request):
    try:
        if ((request.session.get('email') is None) or (request.session.get('email') == "")):
            return HttpResponseRedirect("/home")
        dict_act = {}
        email = request.session.get('email')
        email = request.session.get('email')
        # save_url = save_current_url(request, email)
        # Based on logged in user email filtering list of objects.
        account = vk_Account_history.objects.filter(email=email)
        # Calculating logged in time.
        for i in account:
            some_date = diff_time_Acnt(i)
            dict_act[i.id] = some_date
        return render(request, 'account_security.html', {'account': account, 'fullname': fullname(email),
            some_date = diff_time_Acnt(i)
            dict_act[i.id] = some_date
        return render(request, 'account_security.html', {'account': account, 'fullname': fullname(email),
                                                         'name': first_last_initial(email),
                                                         'time': settings.SESSION_IDLE_TIMEOUT,
                                                         'some_date': dict_act,
                                                         })
    except Exception as e:
        logging.error(e)
        return render(request, 'account_security.html', {'account': account, 'fullname': fullname(email),
        logging.error(e)
        return render(request, 'account_security.html', {'account': account, 'fullname': fullname(email),
                                                         'name': first_last_initial(email),
                                                         'time': settings.SESSION_IDLE_TIMEOUT,
                                                         'some_date': dict_act,
                                                         })
 

Likewise it's happening in other tests also.

Thank you
~Salima

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5e56c8b0-e5f6-9c9b-a9fd-77b5eedcd0d1%40stacktrace.dk.

Kasper Laudrup

unread,
May 19, 2022, 8:24:17 AM5/19/22
to django...@googlegroups.com
On 19/05/2022 05.45, Salima Begum wrote:
> Hi,
> Thank you for your response. We are basically writing a unit test for
> Account_security function with this unit test code.
>
>
> Unit test code :
> --------------------------------------------------------
>
>     def test_Account_security_url(self):
>         path = reverse('account-security')
>         assert resolve(path).view_name == 'account-security'
>
> ---------------------------------------------------------
>
> Here is the function for which we write above unit test, but the test is
> not covering these red colored statements. So, we are wondering what
> could be the issue here.
>
>
> def Account_security(request):
>     try:
>         if ((request.session.get('email') is None) or
> (request.session.get('email') == "")):
>             return HttpResponseRedirect("/home")

You'll always return here since your request doesn't have a session
object with an email so the rest of the code never gets covered.

I suggest you look into the basics of how you can test your Django
views. There's tons of documentation on how to do that out there
including the official Django documentation.

Kind regards,
Kasper Laudrup

Kasper Laudrup

unread,
May 19, 2022, 5:14:23 PM5/19/22
to django...@googlegroups.com
On 19/05/2022 05.45, Salima Begum wrote:
> Hi,
> Thank you for your response. We are basically writing a unit test for Account_security function with this unit test code.
>
>
> Unit test code :
> --------------------------------------------------------
>
>     def test_Account_security_url(self):
>         path = reverse('account-security')
>         assert resolve(path).view_name == 'account-security'
>
> ---------------------------------------------------------
>
> Here is the function for which we write above unit test, but the test is not covering these red colored statements. So, we are wondering what could be the issue here.
>
>
> def Account_security(request):
>     try:
>         if ((request.session.get('email') is None) or
> (request.session.get('email') == "")):
>             return HttpResponseRedirect("/home")

Reply all
Reply to author
Forward
0 new messages