4

This is a generic question geared towards unit testing specifically in Python. Is it acceptable to place 20 or some number of inputs to test in a dictionary as keys, where the values are the expected result output from the function?

I can't find an answer after scouring the internet. Almost everything I read were toy examples like adding numbers to obtain a sum or concatenating a first and last name. The following is another toyish example to describe my question in a simple way.

import unittest
import some_module

class SomeModuleUnitTest(unittest.TestCase):

    def test_some_function(self):

        input_output_dict = {
            1: 1,
            2: 4,
            3: 9,
            4: 16,
            5: 25,
            6: 36,
            7: 49,
              .
              .
              .

            20: 400
        }

        for input_case, output_expected in input_output_dict.items():
            output_actual = some_module.some_function(input_case)
            self.assertEqual(output_expected, output_actual)

If this is considered archaic, what would be the Pythonic solution for testing multiple inputs?

probat
  • 1,422
  • 3
  • 17
  • 33
  • People who down vote could at least share why you do so. I spent over an hour searching for an answer on this in order to follow best practices and couldn't find an answer. – probat Apr 25 '20 at 00:12
  • As for the down vote - I would guess the question is seen as opinion - based and therefore not suitable for SO. As for your question - does the answer to [this question](https://stackoverflow.com/questions/32899/how-do-you-generate-dynamic-parameterized-unit-tests-in-python) help you? Also, you may consider pytest, where parametrization is built in. – MrBean Bremen Apr 25 '20 at 06:42
  • Thanks for the link, that does provide some clarity to my question. I don't see how asking my question is opinionated but I too thought that may be why it was down voted. I asked if what I was doing is Pythonic which I presume an experienced Python developer would know the answer, yes or no. If yes great, if no then what is best practice. There are a lot of questions like this that get up voted, for example the link you included. – probat Apr 26 '20 at 01:20

1 Answers1

4

Ok, I will have a go. Trying to answer the explicit and implicit questions that I see here.

Is it pythonic? If that is the data you have, then putting it in a dictionary is certainly ok. If it gets very large, you could consider reading it from a file, but this is probably not needed in this case.

Is this the best way to test multiple inputs?.
No, there are possibilities to use test parametrization in test frameworks that provide output that is better organized than in your example. The most commonly used is probably the pytest.mark.parametrize decorator in pytest.

Do you need that much different input in your test?
That depends on your functionality. If each of the inputs represents a different scenario, certainly yes. If they represent the same scenario, you may think of typical and edge cases, but usually you won't need many different inputs.

Diogo
  • 590
  • 6
  • 23
MrBean Bremen
  • 14,916
  • 3
  • 26
  • 46