Python unit testing as tos

I am new to Github. I am new to writing Unit Test cases. I contributed to the project, but the owner asked me to provide test modules that do not work before the fix and work after the fix. How can i do this? Should I write them all together? As soon as I have one copy of the code (i.e. With or without correction). I use Python and import unittest. I'm confused. Before the fix, I get an exception, so I have to use assertRaises () for this. I read a lot, but I can not start.

+6
source share
1 answer

Suppose you have a fix for the following delta function:

Broken Version:

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

Fixed Version:

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

Then specify the following test file. It will fail with a broken version and work with a 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 that the project uses the standard unittest module of the library. You must use the structure used by the project.

+5
source

Source: https://habr.com/ru/post/947185/


All Articles