1

I have a file to validate the label

#configmanager.py

class FunctionNode(object):
    def __validate_func_label(self, func_label):
        pattern = re.compile('^fn[1-9][0-9]{0,1}$')
        if not pattern.match(func_label):
            raise Exception(
                f"Invalid value {func_label} for function label in function config.")

and I have to write a test case for it using Unit Testing. Test case will check if user enters fn is between 1 and 99, it will pass. Otherwise if fn out of range such as fn0 or fn100 then it fail. I writed a TestClass

#test__validate_func_label.py

import unittest
from configmanager import FunctionNode

class TestValidateFuncLabel(unittest.TestCase):
    def setUp(self):
        pass

    def test_fn_within_range(self):
        for i in range(1,99):
            self.assertEqual(fn(i),output[i])

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

But I feel like I'm not on the right track. Is there any way i can assert fn is within range from 1 to 99?

Any help is appreciated.

BinhDuong
  • 79
  • 8

2 Answers2

1
self.assertTrue( 0<=y<=99)

where y is the value of fn you are checking.

However, I don't understand how your question relates to your code. In your code, you assert that fn(i) has a certain value (taken from a list named output which is not elsewhere defined), where i takes each value from 1 to 98.

But in your question you ask about asserting that a value is between these numbers.

Joshua Fox
  • 15,727
  • 14
  • 65
  • 108
0

Another solution is:

https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertIn

self.assertIn(fn, range(1, 100))
jamylak
  • 111,593
  • 23
  • 218
  • 220