【问题标题】:Test case data stays in Django development database Selenium测试用例数据保留在 Django 开发数据库 Selenium 中
【发布时间】:2018-08-17 01:25:22
【问题描述】:

我有一个简单的新用户注册表格。我为它写了一个测试用例。它看起来如下:

class AccountTestCase(LiveServerTestCase):

    def setUp(self):
        self.selenium = webdriver.Firefox()
        super(AccountTestCase, self).setUp()

    def tearDown(self):
        self.selenium.quit()
        super(AccountTestCase, self).tearDown()

    def test_register(self):
        selenium = self.selenium
        #Opening the link we want to test
        selenium.get('http://localhost:8000/register/')
        #find the form element
        first_name = selenium.find_element_by_id('id_first_name')
        last_name = selenium.find_element_by_id('id_last_name')
        username = selenium.find_element_by_id('id_username')
        email = selenium.find_element_by_id('id_email')
        password1 = selenium.find_element_by_id('id_password1')
        password2 = selenium.find_element_by_id('id_password2')

        submit = selenium.find_element_by_id('btn_signup')

        #Fill the form with data
        first_name.send_keys('abc')
        last_name.send_keys('abc')
        username.send_keys('abc')
        email.send_keys('abc@gmail.com')
        password1.send_keys('abcabcabc')
        password2.send_keys('abcabcabc')

        #submitting the form
        submit.send_keys(Keys.RETURN)

        #check the returned result
        self.assertTrue('Success!' in selenium.page_source)

当我第一次运行测试用例时,它以优异的成绩通过,但在第二次运行时却失败了。

经过一番调查,我意识到我的数据库中已经创建了一个具有测试用例凭据的用户。因此,当我第二次运行测试用例时,它无法创建新用户,因为具有这些详细信息的用户已经存在。 (我可以看到来自 Django 管理员的用户)。

我相信这不是 LiveServerTestCase(或任何类型的测试用例)的预期行为。在设置中,创建一个临时数据库,在其上运行测试用例,并在拆解阶段销毁。

我想知道,这是否是预期的行为?如果不是为什么会这样?我怎样才能避免这样做?

我没有在 settings.py 中进行任何与硒或测试相关的更改(是否有标志或需要设置的东西?)。另外,我需要保持服务器运行才能正常工作(这正常吗?)。

【问题讨论】:

  • 我认为你的两个问题都是相关的。您需要使用self.live_server_url 作为 URL,并且不需要运行您的开发服务器。您正在那里创建用户,而不是使用测试数据库。这在the documentation 中有描述。

标签: django selenium django-testing


【解决方案1】:

正如@Paulo Almeida 指出的那样:

我使用了错误的网址。我应该使用的 URL 是 self.live_server_url 。由于我使用的是http://localhost:8000/register/,因此期望服务器正在运行并在那里创建记录。

谢谢。

【讨论】:

    猜你喜欢
    • 2011-06-04
    • 2019-10-04
    • 2011-09-23
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 2015-10-27
    • 1970-01-01
    相关资源
    最近更新 更多