Python ';用户更改密码';工程验收试验

Python ';用户更改密码';工程验收试验,python,bdd,acceptance-testing,Python,Bdd,Acceptance Testing,我正在使用behave(python)自动化验收测试,当时我遇到了一个为更改密码场景编写测试的问题 这是我在test\u change\u password.py中的步骤 from behave import * from features.steps.common_steps import fill_in_email, fill_in_password, show_main_page, register_user, logout from features.steps.constants im

我正在使用behave(python)自动化验收测试,当时我遇到了一个为更改密码场景编写测试的问题

这是我在
test\u change\u password.py中的步骤

from behave import *
from features.steps.common_steps import fill_in_email, fill_in_password, show_main_page, register_user, logout
from features.steps.constants import *
import time

CHANGE_PASSWORD_USER_EMAIL = 'change_password@gmail.com'

use_step_matcher("re")


def login(context, password):
    context.browser.visit("/login")
    context.browser.is_element_present_by_css("//input")
    fill_in_email(context, 'test@gmail.com')
    fill_in_password(context, password)
    context.browser.find_by_xpath("//button").first.click()

def change_password_fields_and_logout(context):
    old_pass_field = context.browser.find_by_xpath('//form/div[%s]/input' % OLD_PASS_INDEX)
    old_pass_field.fill(NEW_PASSWORD)
    new_pass_field = context.browser.find_by_xpath('//form/div[%s]/input' % NEW_PASS_INDEX)
    new_pass_field.fill(OLD_PASSWORD)
    confirm_new_pass_field = context.browser.find_by_xpath('//form/div[%s]/input' % CONFIRM_NEW_PASS_INDEX)
    confirm_new_pass_field.fill(OLD_PASSWORD)
    context.browser.find_by_xpath('//button[text()="Submit"]').first.click()
    logout(context)


@given("change password user login")
def step_impl(context):
    context.browser.visit('/login')
    context.browser.is_element_present_by_css('//h1')
    fill_in_email(context, CHANGE_PASSWORD_USER_EMAIL)
    fill_in_password(context, NEW_PASSWORD)
    context.browser.find_by_xpath("//button[@type='submit']").first.click()
    if not context.browser.is_element_present_by_text('first name last name'):
        NEW_PASSWORD, OLD_PASSWORD = OLD_PASSWORD, NEW_PASSWORD
        fill_in_password(context, NEW_PASSWORD)


@when("user changed his password")
def step_impl(context):
    context.browser.is_element_present_by_text('first name last name')
    context.browser.find_by_text("first name last name").first.click()
    context.browser.is_element_present_by_text('Change password')
    context.browser.find_by_xpath('//a[text()="Change password"]').first.click()
    change_password_fields_and_logout(context)


@then("user login with new password")
def step_impl(context):
    login(context, NEW_PASSWORD)
它在我第一次运行测试时按预期工作,但因为它更改了用户在DB中的密码,所以在运行测试之前,我必须交换
NEW\u password
OLD\u password
的值,或者截断DB并为我的装置设置种子

我认为有更好、更自动化的方法来做这件事

  • 连接到数据库并在设置方法中插入新用户
  • 在步骤1中测试更改添加用户的密码
  • 在测试清理中从数据库中删除用户。

    您可以使用设置和清理,也可以编写自己的步骤

  • 是的,您希望在测试之间截断数据库,或者实现显式的行为步骤,允许您以某种方式清理数据库。如何做到这一点取决于您的DB实现和其他因素,这些因素在您的问题中需要更多的细节,并且可能超出堆栈溢出的范围。搜索有关使用Behave进行事务性DB测试的教程。