简体   繁体   中英

Unit-testing a function

I am new to unit-testing. However, I need to make a test for the function below. I've done some reading but can't really get much from reading. This is what I have been able to come with but I certainly know there is more to do. Could someone tell me what i would need to do next.

def apply_filter(x): 
    filterer = { 
        1: 'ether proto 0x88B8', 
        2: 'tcp port 102', 
        3: 'ether proto 0x88BA' 
    } 
    return filterer.get(x, '') 



import unittest 

from new_format import apply_filter 


class test_apply_filter(unittest.TestCase): 

    def setUp(self): 
        pass 
    def tearDown(self): 
        pass 

    def test_filter_by_name(self): 
        self.assertEqual(apply_filter(1),"ether proto 0x88B8") 
        self.assertEqual(apply_filter(2),"tcp port 102") 
        self.assertEqual(apply_filter(3),"ether proto 0x88BA") 

if __name__ == '__main__': 
    unittest.main()

A good way to write unit tests is to write the test before writing the code, then you write the code to make the test turn green, then you clean your code. It's call TDD, Test-driven development.

You can learn more about TDD here: wikipedia

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