2012-01-26 131 views
3

我喜欢在提交之前运行测试。 我是新来的Selenium,我不明白如何运行测试,而不是更改数据库。如何针对测试数据库运行Selenium测试?

我的本地数据库有几十个相同的发布的问题。

有没有什么办法可以使这些测试运行,而不是将数据库恢复到tearDown上的原始状态?

from selenium import webdriver 
from django.utils import unittest 
from selenium.webdriver.support.ui import WebDriverWait 

class TestAuthentication(unittest.TestCase): 
    scheme = 'http' 
    host = 'localhost' 
    port = '4444' 


    def setUp(self): 
     self._driver = webdriver.Firefox() 
     self._driver.implicitly_wait(5) 

    def login_as_Bryan(self): 
     self._driver.get('http://localhost:8000/account/signin/') 
     user = self._driver.find_element_by_id('id_username') 
     user.send_keys("Bryan") 
     password = self._driver.find_element_by_id('id_password') 
     password.send_keys('***************') 
     submit = self._driver.find_element_by_id('blogin') 
     submit.click() 

    def test_user_should_be_able_to_login_manually(self): 
     self.login_as_Bryan(self) 
     message = self._driver.find_element_by_class_name('darkred') 
     self.assertEqual("Welcome back Bryan, you are now logged in", message.text) 

    def test_Bryan_can_post_question(self): 
     self.login_as_Bryan() 
     self._driver.find_element_by_link_text("ask a question").click() 
     self._driver.find_element_by_id('id_title').send_keys("Question should succeed") 
     self._driver.find_element_by_id('editor').send_keys("This is the body text.") 
     self._driver.find_element_by_id('id_tags').send_keys("test") 
     self._driver.find_element_by_class_name("submit").click() 
     self.assertTrue(self._driver.find_element_by_link_text("Question should succeed")) 

    def tearDown(self): 
     self._driver.quit() 

回答

1

问题不在于Selenium,因为它是您的执行环境。这取决于你如何激活你的应用程序。

通常,您需要引导启动应用程序,以便它指向仅在该测试期间使用的临时数据库。测试执行后,您应该删除该数据库。

或者,您可以在您的实际网站中提供UI机制来清除/刷新测试数据库。在这种情况下,您仍然需要一个测试数据库,但是您不需要在每次测试执行时删除/重新创建它。

相关问题