Posts

Showing posts with the label table driven test

Table Driven Unit Test in Python

Now days, table driven tests are pretty much industry standard. In my workplace, we use table driven tests when we write unit tests (in golang though). Here I shall share a simple code example using pytest that shows how to write table driven tests in Python. In table driven test, what you need to do is, to gather all the tests cases together in a single table. We can use dictionary for each test case and a list to store all the test cases. Instead of discussing it further, let me show you an example : def average(L): if not L: return None return sum(L)/len(L) def test_average(): test_cases = [ { "name": "simple case 1", "input": [1, 2, 3], "expected": 2.0 }, { "name": "simple case 2", "input": [1, 2, 3, 4], "expected": 2.5 }, { "name": "list w...