如何在Python中使用Unittest断言整数在范围内?
我有一个文件来验证标签如何在Python中使用Unittest断言整数在范围内?,python,python-unittest,Python,Python Unittest,我有一个文件来验证标签 #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"I
#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.")
我必须使用单元测试为它编写一个测试用例。测试用例将检查用户输入的fn
是否在1和99之间,它将通过。否则,如果fn
超出范围,例如fn0
或fn100
则失败。我写了一个测试类
#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()
但我觉得我没有走上正轨。
是否有任何方法可以断言fn
在1到99之间
感谢您的帮助。self.assertTrue(0另一种解决方案是:
self.assertTrue( 0<=y<=99)
self.assertIn(fn, range(1, 100))