Skip to main content
 首页 » 编程设计

python 单元测试我的密码验证代码

2024年12月31日11lhb25

<分区>

我有这个功能代码:

class PasswordChecker(object): 
 
 passwords = ["aD23"] 
 
 def check(self, passwords): 
     if not passwords: 
         raise NameError('You have not entered any password') 
     else: 
         pass 

还有这个测试代码:

import unittest 
 
from app.passwordchecker import PasswordChecker 
 
class TestPasswordChecker(unittest.TestCase): 
  def setUp(self): 
    self.pswd = PasswordChecker() 
 
  def test_password_checker_raises_nameerror_for_noargs(self): 
    self.assertRaises(NameError, self.pswd.check, []) 

我希望当通过在 self.assertRaises() 中将 [] 替换为 [items] 使测试失败时,它应该抛出一个 NameError - 你没有输入任何密码。但是,它抛出一个不同的错误,这意味着我的代码有问题。请帮忙