简体   繁体   English

单元测试 Python

[英]Unittest Python

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 )所以对于我的 unittest 脚本,我有这样的东西:(忽略语法,我正在学习 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?单元测试好吗?

First things first: I think it is a good idea to test your code and to familiarize yourself with something like unittest .首先要做的事情:我认为测试您的代码并熟悉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 string s, but add an if statement that is only executed when these two variables are not string s.您将IpAddmachineName设置为string s,但添加了一个if语句,该语句仅在这两个变量不是string s 时执行。 Therefore, it looks like the self.assertRaises is not executed.因此,看起来self.assertRaises没有被执行。

  2. assertRaises tends to expect an exception, which tends not to be raised when passing valid parameters. assertRaises倾向于预期异常,在传递有效参数时往往不会引发异常。 Therefore, your two commented self.assertTrue(...) statements tend to more reasonable in the two cases you provided.因此,在您提供的两种情况下,您的两个注释self.assertTrue(...)语句往往更合理。


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.我希望这对您以后的步骤有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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