Introduction

After reviewing what is at the gpx_reader branch I've found #TODO that said about making mocks, but it was not best name for what I've intended to do. I've wanted to parameterized the TestCase, so I could use different type of test-source data for one and the same test source-code.

In my professional work I've already done something similar, so figuring this out was not hard, hence I've found the parameterized library and used it.

Prerequisites - How to use Parameterized

Parameterized is a python library that usage is pretty simple.

1. Install Parameterized with pip:

pip install parameterized

2. Import Parameterized in your tests*.py files:

from parameterized import parameterized

3. Usage - here we go!

So how to use it?

Well taking into account I'm working with django.TestCase that is a deriviation of unittest.TestCase I found information in Parameterized of usage that said:

import unittest
from parameterized import parameterized

class AddTestCase(unittest.TestCase):
    @parameterized.expand([
        ("2 and 3", 2, 3, 5),
        ("3 and 5", 2, 3, 5),
    ])
    def test_add(self, _, a, b, expected):
        assert_equal(a + b, expected)

For other usages you may find Parameterized Projects in Github as a documentation.

What I've done with code:

In my case I've wanted to include parameters for gpx_file at setUp, but it was to hard and time-consuming to find out how to make it work.

A semi solution I've found that is not pretty, but works is as presented below:

class GPXReaderTestCase(TestCase):
    """
    Tests all GPXReader class cases for methods access.
    """
    @parameterized.expand([
        ('bikingendorphines/example_data/15212277.gpx'),
    ])
    def test_get_points(self, gpx_file):
        """
        Tests if there will be data output from get_points.

        Test if:
        - points exists for different types.
        - what will happen if types exists, but no data?
        """
        self.gpxreader = GPXReader(gpx_file)
        points = []
        for point in self.gpxreader.get_points():
            points.append(point)
        self.assertGreater(len(points), 0)

Why this code is not pretty??

Well, you find yourself in situation, when for each test you will have to add information about GPXReader. Using setUp with this field would make creating code a little bit easier to maintain and read.

I'll probably go back to this code and use the cleaner pytest base which may clear this small issue.

Code commits done for this post:

Adds mocking of gpx file using parameterized.

Tools and applications used:

LL (Lessons Learned)

1. Pick tools for what they are intented for.

I've forgot that default django.TestCase are for testing Django-framework itself behaviours like ORM inside of django and other.

They are not ideal for unit-testing and for that you should use something different.

Solution:

For unit-testing better to use one of those :

Acknowledgements

What's next

1. Change behaviour of GPXReaderTestCase to more pytest alike and add some more test code

2. Push finally first version of GPX Reader to master via pull-request.

3. Check Issues of this project.



Comments

comments powered by Disqus