Skip to content

Supertype Fellowship Challenge

Samuel Chan edited this page Mar 1, 2023 · 2 revisions

This Wiki page contains instructions specific to members of Supertype Fellowship.

Supertype Fellowship's Unit Test Challenge

To successfully complete a Challenge and obtain the corresponding badges, you must submit a Pull Request to this repository that either:

  • Increases the test coverage of the codebase by at least 2%
  • Improve the quality of existing tests written by others (e.g. by adding more assertions, or by making the tests more robust, remove duplication or trivial tests, etc.) in a meaningful capacity
    • Your mentor will be able to provide you with focused feedback and guidance on this

Needless to say, your own tests should also be non-trivial (e.g. a test that is testing a single line of code, or a test that is testing a trivial function, or random pass tests etc.) and should be meaningful.

Meaningful Tests take thoughts and effort

from emailnetwork.summary import DomainSummary

class TestSummary(TestCase):
    def setUp(self):
        self.domain_summary = DomainSummary(self.reader)

    def test_example_0(self):
        self.assertTrue(isinstance(list(self.domain_summary.recipients), list))

    def test_example_1(self):
        self.assertTrue(isinstance(self.domain_summary, DomainSummary))

    def test_example_2(self):
        for summary in self.domain_summary.summary:
            self.assertTrue(isinstance(summary, str))

    def test_example_3(self):
        for email in self.domain_summary.emails:
            self.assertGreater(len(email.repicient), 0)

    def test_example_4(self):
        for email in self.domain_summary.emails:
            self.assertIn(keys, ('Incoming', 'Outgoing'))

Tests like test_example_0 are faulty and pointless. They are not testing anything meaningful, since list(any_iterables) will return a list and this is Python's default behavior. To test this is to test Python's default behavior, which is not the purpose of unit tests.

Tests like test_example_1 and test_example_2 are similarly pointless. They are not testing anything meaningful as it is testing the type of the object, which is not the purpose of unit test (unless you are testing a type checker). You want to test for intended behavior of the code, not the type of the object.

Tests like test_example_3 are better, as they do test against the intended behavior of the code. As every email must have at least one recipient (it can have more than one, but it must have at least one), the test is testing that the length of the recipient list is greater than 0. This is a meaningful test. In fact, this test, if passed, would imply that it is an iterable that can be converted to a list, hence defeating the purpose of test_example_0 and perhaps also test_example_1. This is a good example of how tests can be redundant and how they can be improved.

Tests like test_example_4 are also considered to be valid tests. It is better than testing the instance of the object being a dictionary. It tests that the necessary keys are in place for the dictionary, since every email has an incoming and outgoing count. It can be improved however, by directly testing the expected values of the keys, instead of just testing that the keys are present.

Tutorial Videos on writing Unit Tests

You can watch the videos (despite not building the same project) below, as it goes into how I formulate unit tests while building up a project; It would be a great reference to you writing out your own tests.