Home > Software design >  Unittest Python
Unittest Python

Time:09-30

No much experience in unit tests , someone can help me to review o explain what is expected here.I know concept on unittest, but confused with operacional functioning.

So fo example I have one script with this .

def validateName(machineName):
    if machineName:
        pattern = "^[a-z][a-z\d-] $$"
        if not re.match(pattern,machineName):
           return False 
    return True


def validateIp(inputIp):
    try:
        ipaddress.ip_address(inputIp)
    except ValueError as err:
        raise(err)  

So for my unittest script i have something like this: (disregard syntax, i am learning unittest )

class TestValidateF(unittest.TestCase):
    def test_validateIpAddress(self):
        IPAdd = "10.75.10.98"
        if isinstance(IPAdd, str) == False:
            message="Error Parameter is empty or has an ivalidad format. Verify it does not star with hiphen "
        #self.assertTrue(ValideIPAdd(IPAdd),True)
            self.assertRaises(IPAdd, message)
    
def test_validateMachineName(self):
    sitename = "machineName"
    if isinstance(machineName, str) == False:
        message="Error Parameter is empty or has an ivalidad format."
    #self.assertTrue(machineName(machineName),True)
        self.assertRaises(machineName, message)

Is fine the unittest?

CodePudding user response:

First things first: I think it is a good idea to test your code and to familiarize yourself with something like unittest. Also, it is good that you are testing only one thing per test case. Therefore, in general you are on the right track.

However, in your special case I think currently you are not testing anything with your two test cases.

  1. You are setting IpAdd and machineName as strings, but add an if statement that is only executed when these two variables are not strings. Therefore, it looks like the self.assertRaises is not executed.

  2. assertRaises tends to expect an exception, which tends not to be raised when passing valid parameters. Therefore, your two commented self.assertTrue(...) statements tend to more reasonable in the two cases you provided.


def test_validateIpAddress(self):
        IPAdd = "10.75.10.98"
        self.assertTrue(ValideIPAdd(IPAdd))
  1. Try to cover all multiple pathes by also testing if invalid parameters are handled correctly.

def test_invalid_machine_name(self):
        machineName = '9-not_valid%' 
        self.assertFalse(machineName(machineName))
  1. Test if exceptions are raised when you are expecting them:

def test_validateIpAddress(self):
        IPAdd = None
        with self.assertRaises(ValueError):
                ValideIPAdd(IPAdd)

Some more general tips on how to write test cases I personally found where helpful can be found here. Furthermore, I think it is a good idea to familiarize yourself early in your learning endeavor with mocks and patches. It makes test cases cleaner and allows you to concentrate on important points of your code.

I hope that helps in your future steps.

  • Related