testing 101

or How I Learned to Stop Worrying and Love the Tests

Why should we test?

- tests validate your code works as expected

 

- tests guarantee a specific behavior in a specific moment of the development process

- tests avoid errors in your code created by other people's code

 

- tests will find bugs or malfunctions inside your code

 

"The best part is, it’s really easy."

"You can verify your code is doing what it should be doing"

"Testing a Web application is a complex task, because a Web application is made of several layers of logic"

from django.test import TestCase
from myapp.models import Animal

class AnimalTestCase(TestCase):
    def setUp(self):
        Animal.objects.create(name="lion", sound="roar")
        Animal.objects.create(name="cat", sound="meow")

    def test_animals_can_speak(self):
        """Animals that can speak are correctly identified"""
        lion = Animal.objects.get(name="lion")
        cat = Animal.objects.get(name="cat")
        self.assertEqual(lion.speak(), 'The lion says "roar"')
        self.assertEqual(cat.speak(), 'The cat says "meow"')

"Testing a Web application is a complex task, because a Web application is made of several layers of logic"

from django.test import TestCase, Client


class ProjectTestCase(TestCase):
    
    def setUp(self):
        self.client = Client()

    def test_project_view_status(self):
        """
        When accessed, ProjectView should return a http 200
        status code
        """
        response = self.client.get(reverse('projects:index'))
        self.assertEqual(response.status_code, 200)

"Testing a Web application is a complex task, because a Web application is made of several layers of logic"

from django.test import TestCase, Client
from .models import Customer

class SimpleTest(TestCase):
    def setUp(self):
        self.client = Client()
        Customer.objects.create(name='customer 1')
        # creates 4 customers more.        

    def test_details(self):
        # Issue a GET request.
        response = self.client.get('/customer/details/')

        # Check that the response is 200 OK.
        self.assertEqual(response.status_code, 200)

        # Check that the rendered context contains 5 customers.
        self.assertEqual(len(response.context['customers']), 5)

"Testing a Web application is a complex task, because a Web application is made of several layers of logic"

from django.test import TestCase, Client
from .utils import add_function

class SimpleTest(TestCase):

    def test_function(self):

        self.assertEqual(add_function(3,2), 5)
        self.assertEqual(add_function(3,5), 8)

        self.assertEqual(add_function(-3,2), 1)
        self.assertEqual(add_function(3,-5), -2)

        self.assertEqual(add_function(3.35,2.2), 5.55)
        self.assertEqual(add_function(3.5,-2), 1.5)
        self.assertEqual(add_function(3,7), 11) #fails

Useful tips:

- Blank databases are created for the tests.

- A test may fail or may raise an error; In both cases It did not pass, but they implies different things.

- Write a separate test method for each set of conditions you want to test.

- write test method names that describe their function.

- in testing redundancy is a good thing.

# Run all the tests in the animals.tests module
$ ./manage.py test animals.tests

# Run all the tests found within the 'animals' package
$ ./manage.py test animals

# Run just one test case
$ ./manage.py test animals.tests.AnimalTestCase

# Run just one test method
$ ./manage.py test animals.tests.AnimalTestCase.test_animals_can_speak

Coverage

$ coverage report -m
Name                      Stmts   Miss  Cover   Missing
-------------------------------------------------------
my_program.py                20      4    80%   33-35, 39
my_other_module.py           56      6    89%   17-23
-------------------------------------------------------
TOTAL                        76     10    87%

It monitors your program, noting which parts of the code have been executed, then analyzes the source to identify code that could have been executed but was not (...) It can show which parts of your code are being exercised by tests, and which are not

Thank you

Copy of testing 101

By swapps

Copy of testing 101

  • 556