1

我正在为我的应用程序创建 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."""
4

1 回答 1

0

如果您使用 Selenium,django.utils.unittest.TestCase请不要使用django.test.TransactionalTestCase,甚至更好,LiveServerTestCase.

于 2013-04-13T20:43:41.637 回答