2012-01-26 12 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

問題はあなたの実行環境のようにあまりセレンではありません。あなたのアプリケーションをどのように起動するかによって異なります。

一般に、アプリケーションの起動は、そのテスト中にのみ使用する一時データベースを指し示すようにブートストラップする必要があります。テストの実行後、そのデータベースを削除する必要があります。

また、実際のWebサイトにUIメカニズムを提供して、テストデータベースをクリア/リフレッシュすることもできます。その場合でも、まだテストデータベースが必要ですが、テストを実行するたびに削除/再作成する必要はありません。

0

django-seleniumを使用し、テストデータベースのテストを実行します。

関連する問題