Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用pytest编写正确的测试?_Python_Unit Testing_Testing_Automated Tests_Pytest - Fatal编程技术网

Python 如何使用pytest编写正确的测试?

Python 如何使用pytest编写正确的测试?,python,unit-testing,testing,automated-tests,pytest,Python,Unit Testing,Testing,Automated Tests,Pytest,我可以编写一些单元测试,但不知道如何编写有关将其他函数连接在一起的createAccount()的测试 createAccount()按顺序包含一些步骤: 验证电子邮件 验证密码 检查密码是否匹配 实例化新帐户对象 每个步骤都有一些测试用例。 因此,我的问题是: 1.如何编写createAccount()测试用例?我应该列出所有可能的组合测试用例,然后测试它们吗 例如: 测试用例0。电子邮件无效 测试用例1。应用程序在重试电子邮件3次后停止 测试用例2。电子邮件正常,密码无效 测试用例3。电子邮

我可以编写一些单元测试,但不知道如何编写有关将其他函数连接在一起的createAccount()的测试

createAccount()按顺序包含一些步骤:

  • 验证电子邮件

  • 验证密码

  • 检查密码是否匹配

  • 实例化新帐户对象

  • 每个步骤都有一些测试用例。 因此,我的问题是: 1.如何编写createAccount()测试用例?我应该列出所有可能的组合测试用例,然后测试它们吗

    例如:

    测试用例0。电子邮件无效

    测试用例1。应用程序在重试电子邮件3次后停止

    测试用例2。电子邮件正常,密码无效

    测试用例3。电子邮件正常,密码有效,第二个密码与第一个密码不匹配

    测试用例4。电子邮件正常,密码有效,两者密码匹配,安全性有效

    测试用例5。电子邮件正常,密码有效,两者密码匹配,安全性有效,帐户创建成功

  • 我不知道如何测试吗,因为我的createAccount()很烂?如果是,如何重构它以便于测试
  • 这是我的代码:

    类注册表:
    def getEmail(自我):
    返回输入(“请键入电子邮件:”)
    def getPassword1(自身):
    返回输入(“请键入密码:”)
    def getPassword2(自身):
    返回输入(“请确认您的密码:”)
    def getSecKey(自):
    返回输入(“请键入您的安全关键字:”)
    def打印消息(自我,消息):
    打印(信息)
    类注册控制器:
    定义初始化(自,视图):
    self.view=view
    def显示消息(自我,消息):
    self.view.printMessage(消息)
    def ValidateEmail(自我、电子邮件):
    “”“从用户处获取电子邮件,请检查电子邮件
    """
    self.email=电子邮件
    email\u obj=电子邮件(self.email)
    状态=email_obj.isValidEmail()而不是accounts.isDuplicate(self.email)
    如果不是状态:
    提出EmailNotOK(“电子邮件重复或格式不正确”)
    其他:
    返回真值
    def ValidatePassword(自我,密码):
    """
    从用户处获取密码,检查密码是否有效
    """
    self.password=密码
    状态=密码.isValidPassword(self.Password)
    如果不是状态:
    提升密码无效(“密码无效”)
    否则:返回True
    def CheckPasswordMatch(自身、密码):
    """
    从用户处获取密码2,检查密码匹配
    """
    密码2=密码
    状态=Password.isMatch(self.Password,Password_2)
    如果不是状态:
    提高PassNotMatch(“Pass不匹配”)
    否则:返回True
    def CreateCount(自我):
    重试=0
    而1:
    尝试:
    email\u input=self.view.getEmail()
    self.ValidateEmail(电子邮件输入)#
    打破
    除电子邮件外,电子邮件地址为e:
    重试=重试+1
    self.displaymessage(str(e))
    如果重试>3:
    返回
    而1:
    尝试:
    password1_input=self.view.getPassword1()
    self.ValidatePassword(密码1\u输入)
    打破
    除PassNot作为e有效外:
    self.displaymessage(str(e))
    而1:
    尝试:
    password2_input=self.view.getPassword2()
    self.CheckPasswordMatch(password2\u输入)
    打破
    除PassNotMatch作为e外:
    self.displaymessage(str(e))
    self.seckey=self.view.getSecKey()
    帐户=帐户(电子邮件(self.Email)、密码(self.Password)、self.seckey)
    message=“已成功创建帐户”
    self.displaymessage(消息)
    返回帐户
    类别登记册(选项):
    def执行(自我):
    view=RegisterUI()
    控制器\一个=寄存器控制器(视图)
    控制器_one.createAccount()
    “”“==================================================================================================================================“””
    “测试”
    @pytest.fixture(scope=“session”)
    def ctrl():
    view=RegisterUI()
    返回寄存器控制器(视图)
    def test_CanthRowroreMailNotValid(ctrl):
    电子邮件='dddddd'
    使用pytest.raises(EmailNotOK)作为e:
    ctrl.ValidateEmail(电子邮件)
    断言str(e.value)=“电子邮件重复或格式不正确”
    def测试电子邮件是否有效(ctrl):
    电子邮件hello@gmail.com'
    断言ctrl.ValidateEmail(电子邮件)==True
    def测试\u CanthRowerPassNotValid(ctrl):
    密码='123'
    使用pytest.raises(PassNotValid)作为e:
    ctrl.ValidatePassword(密码)
    断言str(e.value)=“传递无效”
    def test_密码有效(ctrl):
    密码='1234567'
    断言ctrl.ValidatePassword(密码)==True
    def测试\u CanthRowerPassNotMatch(ctrl):
    密码1='1234567'
    ctrl.password=password1
    password2='abcdf'
    使用pytest.raises(PassNotMatch)作为e:
    ctrl.CheckPasswordMatch(密码2)
    断言str(e.value)=“传递不匹配”
    def测试\u密码匹配(ctrl):
    密码1='1234567'
    ctrl.password=password1
    密码2='1234567'
    断言ctrl.CheckPasswordMatch(密码2)
    
    注意:我不太懂Python,但我知道测试。我的Python可能不完全正确,但技术是正确的


    答案在于您对
    createAccount
    的描述。它做的事情太多了。它对各种验证方法进行了包装。它显示消息。它创建一个帐户。它需要重构才能进行测试。测试和重构是齐头并进的

    首先,对这四个部分中的每一个都执行一个测试,将它们转换为各自的方法。我只做三个验证步骤中的一个,它们基本上都是一样的。因为这是一个死记硬背的操作,所以我们可以安全地进行

    只要看看这段代码就会发现一个bug:
    createAccount
    不会停止
    def tryValidatePassword(self):
        while 1:
            try:
                password1_input = self.view.getPassword1()
                self.ValidatePassword(password1_input)
                break
            except PassNotValid as e:
                self.displaymessage(str(e))
    
    def makeAccount(self):
        return Account(Email(self.email), Password(self.password), self.seckey)
    
    def createAccount(self):
        self.tryValidatePassword()
    
        self.seckey = self.view.getSecKey()
        account = self.makeAccount()
        message = "Account was create successfully"
        self.displaymessage(message)
        return account    
    
        def tryValidatePassword(self):
            try:
                password1_input = self.view.getPassword1()
                self.ValidatePassword(password1_input)
            except PassNotValid as e:
                self.displaymessage(str(e))
    
        def ValidatePassword(self, password):
            """
            get password from user, check pass valid
            """
            self.password = password
            return Password.isValidPassword(self.password)
    
        def ValidatePassword(self, password):
            return Password.isValidPassword(self.password)
    
        def tryValidatePassword(self):
            password1_input = self.view.getPassword1()
            if !self.ValidatePassword(password1_input):
                self.displaymessage("Pass isn't valid")
    
        def tryValidatePassword(self, password):
            if !self.ValidatePassword(password):
                self.displaymessage("Pass isn't valid")
    
        self.tryValidatePassword(self.view.getPassword1())
    
    class RegisterController:
        # Thin wrappers to hide the details of the validation implementations.
        def ValidatePassword(self, password):
            return Password.isValidPassword(password)
    
        # If there needs to be retries, they would happen in here.
        def ValidateEmail(self, email_string):
            email = Email(email_string)
            return email.isValidEmail() and not accounts.isDuplicate(email_string)
    
        def CheckPasswordMatch(self, password1, password2):
            return Password.isMatch(password1, password2)
    
        # A thin wrapper to actually make the account from valid input.
        def makeAccount(self, email, password, seckey):
            return Account(Email(email), Password(password), seckey)
    
        def createAccount(self):
            password1 = self.view.getPassword1()
            password2 = self.view.getPassword2()
    
            if !self.ValidatePassword(password1):
                self.displaymessage("Password is not valid")
                return
    
            if !self.CheckPasswordMatch(password1, password2):
                self.displaymessage("Passwords don't match")
                return
    
            email = self.view.getEmail()
            if !self.ValidateEmail(email):
                self.displaymessage("Email is duplicate or incorrect format")
                return
    
            account = self.makeAccount(email, password, self.view.getSecKey())
            self.displaymessage("Account was created successfully")
            return
    
    # This is just a sketch.
    
    class CreateAccountFailed(Exception):
        pass
    
    class PassNotValid(CreateAccountFailed):
        pass
    
    class PassNotMatch(CreateAccountFailed):
        pass
    
    class EmailNotOK(CreateAccountFailed):
        pass
    
        def createAccount(self):
            password1 = self.view.getPassword1()
            password2 = self.view.getPassword2()
    
            if !self.ValidatePassword(password1):
                raise PassNotValid("Password is not valid")
    
            if !self.CheckPasswordMatch(password1, password2):
                raise PassNotMatch("Passwords don't match")
    
            email = self.view.getEmail()
            if !self.ValidateEmail(email):
                raise EmailNotOK("Email is duplicate or incorrect format")
    
            return self.makeAccount(email, password, self.view.getSecKey())
    
        # A thin wrapper to handle the display.
        def tryCreateAccount(self):
            try
                account = self.createAccount()
                self.displaymessage("Account was created successfully")
                return account
            except CreateAccountFailed as e:
                self.displaymessage(str(e))