我正在为我的App创建Selenium测试。 我可以创建一个新用户,但我似乎无法弄清楚如何从数据库中删除它。
第一次成功运行测试后,后续测试失败,因为用户名已经存在。
为什么我无法在调试器中查询新创建的记录,尽管能够在页面上看到新记录?
如何在测试中从数据库中删除记录?
这就是我一直在做的事情:
from selenium import webdriver
from django.utils import unittest
from forum.models import Question, Answer, User
class TestOSQAAuthentication(unittest.TestCase):
scheme = 'http'
host = 'localhost'
port = '4444'
def setUp(self):
self._driver = webdriver.Firefox()
self._driver.implicitly_wait(25)
def test_anon_can_create_new_account_manually(self):
self._driver.get('http://localhost:8000/account/local/register/')
self._driver.find_element_by_id('id_username').send_keys('MrManual')
self._driver.find_element_by_id('id_email').send_keys('test@gmail.com')
self._driver.find_element_by_id('id_password1').send_keys('test')
self._driver.find_element_by_id('id_password2').send_keys('test')
self._driver.find_element_by_id('bnewaccount').click()
# verify MrManual was created
self._driver.get('http://localhost:8000/users/')
self._driver.find_element_by_link_text('MrManual')
# MrManual seems to be created, but I don't see MrManual in the database during debugging with:
# import ipdb; ipdb.set_trace()
#ipdb> User.objects.all()
#[<User: Bryan>, <User: Kallie>, <User: Stalin>]
# here I am trying to delete the user from the database directly.
User.objects.filter(username="MrManual").delete()
"""For some reason I can't delete the record from the database from the test.
Selenium can find the new user in the browser, but I can't query the database to find it."""
答案 0 :(得分:0)
如果您使用的是Selenium而不是django.utils.unittest.TestCase
,请使用django.test.TransactionalTestCase
或更好,LiveServerTestCase
。