unit testing python how tos
Question: I am new to Github. I am new to writing Unit Test Cases. I have contributed to
a project but the owner has asked me to provide unit testcases that fail
before the fix and work after the fix. How can I go about doing it? Shall I
write them all together? As at one time I will have one copy of the code (i.e
with fix or without fix). I am using Python and importing unittest. I am
confused. Before the fix I get an exception so should I use assertRaises() for
that. I did read a lot but am not able to start.


Answer: Assume you have a fix for following broken `delta` function:

Broken version:

    
    
    def delta(a, b):
        return a - b
    

Fixed version:

    
    
    def delta(a, b):
        return abs(a - b)
    

Then, provide following testcase. It will fail with the broken version, and
work with the fixed version.

    
    
    import unittest
    
    from module_you_fixed import delta
    
    class TestDelta(unittest.TestCase):
        def test_delta(self):
            self.assertEqual(delta(9, 7), 2)
            self.assertEqual(delta(2, 5), 3)
    
    if __name__ == '__main__':
        unittest.main()
    

I assumed the project use standard library unittest module. You should use the
framework that the project use.

