1

セレンを使用してdjangoアプリケーションをテストしていますが、コードは次のようになります。

class MyLiveServerTestCase(LiveServerTestCase):

    def setUp(self):
        super(MyLiveServerTestCase, self).setUp()
        self.client = Client()
        self.browser = webdriver.Firefox()
        self.browser.implicitly_wait(30)
        self.browser.stop = lambda x: WebDriverWait(self.browser, x).until(lambda x: False)
        self.browser.get_body = lambda: self.browser.find_element_by_tag_name('body')

    def tearDown(self):
        super(MyLiveServerTestCase, self).tearDown()
        self.browser.quit()

    def login_staff(self):
        User.objects.create_superuser('abc', 'abc@gmail.com', '1234')
        # User opens his web browser, and goes to the website
        self.browser.get(self.live_server_url)
        # User types in his username and passwords and hits return
        username_field = self.browser.find_element_by_name('username')
        username_field.send_keys('abc')
        password_field = self.browser.find_element_by_name('password')
        password_field.send_keys('1234')
        # User submits the form
        password_field.send_keys(Keys.RETURN)
        body = self.browser.find_element_by_tag_name('body')
        self.assertIn('abc', body.text)

に対してこのテスト ケースを実行するとlogin_staff、エラーが発生します

IntegrityError: UNIQUE 制約が失敗しました: auth_user.username

すべてのテストケースの開始時に、新しいデータベースとユーザーインスタンスを作成しているので、なぜこの整合性エラーが発生するのでしょうか? ここで以前のデータベースをフラッシュする方法はありますか? このテストの実行中に新しいデータベースが作成されたにもかかわらず、このエラーが発生し続ける理由は何ですか?

4

0 に答える 0