简体   繁体   中英

How do I write a program out of provided unittest

I have a unittests for a program. Being new to test driven development, how can I generate a program from the given tests

For example I have this test:

class MaxMinTest(TestCase):
"""docstring for MaxMinTest"""

def test_find_max_min_four(self):
    self.assertListEqual([1, 4],
                         find_max_min([1, 2, 3, 4]),
                         msg='should return [1,4] for [1, 2, 3, 4]')

def test_find_max_min_one(self):
    self.assertListEqual([4, 6],
                         find_max_min([6, 4]),
                         msg='should return [4, 6] for [6, 4]')

def test_find_max_min_two(self):
    self.assertListEqual([2, 78],
                         find_max_min([4, 66, 6, 44, 7, 78, 8, 68, 2]),
                         msg='should return [2, 78] for [4, 66, 6, 44, 7, 78, 8, 68, 2]')

def test_find_max_min_three(self):
    self.assertListEqual([1, 4],
                         find_max_min([1, 2, 3, 4]),
                         msg='should return [1,4] for [1, 2, 3, 4]')

def test_find_max_min_identity(self):
    self.assertListEqual([4],
                         find_max_min([4, 4, 4, 4]),
                         msg='Return the number of elements in the list in a new list if the `min` and `max` are equal')

Thanks in advance. Please also share resources for learning test driven development for a beginner like me.

Without giving you the answer to the code that would make these tests pass, your approach can be something like this:

  • What are you testing?

  • What does this function take, and what does it return?

  • How do I make one test pass? Once I make one test pass, how do I make the other test pass, while ensuring the previous test still passes. And so on, until all tests pass with the code you have written in your function.

So, to answer your questions at a high level.

You are testing something that is called MinMax , which hints out at finding the minimum and maximum of something. This something is a function called find_max_min takes as input a list , and returns a list with two values. These two values are in order, the min and the max of the list you are passing to your function.

Documentation to look at:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM