Search code examples
pythonfunctiondictionarytestingassert

Testing Functions for Program


I have a task where I need to create a series of functions that will test a program given to us. The only tests I need to provide are ones that will come out to True

I followed an example and tried to apply it to the specifications that were given but I'm not sure how to tackle the other ones. This is a very new topic for us in Python and I'd like some help because this will help me learn it a lot faster. Any help is really appreciated. Please show the change in code. Thanks in advance!


Solution

  • The idea of writing a test is generally to pass a complete set of inputs to the function you're testing and verify that they produce the expected output. For your function this might look something like:

    def test_pumpernickel():
        # Test some invalid argument types.
        assert pumpernickel(1, 2, 3) is None
        assert pumpernickel([1, 1.5], [2, 2.5], 3) is None
        assert pumpernickel("foo", "bar", "baz") is None
    
        # Test some True results.
        assert pumpernickel([1], [2, 3], 4)           # 1+3=4
        assert pumpernickel([2, 3], [4, 5, 6], 8)     # 3+5=8
        assert pumpernickel([0, 0, 0], [1, 1, 1], 1)  # 0+1=1
    
        # Test some False results.
        assert pumpernickel([1], [2, 3], 5) is False
        assert pumpernickel([], [3], 3) is False
        assert pumpernickel([1, 2, 3], [4, 5, 6], 0) is False
    

    If any of the tests fail, it tells you that there's a bug in the function (or maybe that there's a bug in the test -- you need to double-check your understanding of what the function is supposed to do, and how the test is working, to know which it is)!