Вопрос

I am facing a strange problem. I am able to login to admin by running python manage.py runserver and giving the correct credentials

but my test fails, if I give the exact same credentials that I used in development server. I am following this TTD tutorial.

Here is the code:

class AdminTest(LiveServerTestCase):
fixtures = ['admin.json']
def setUp(self):
    self.browser = webdriver.Firefox()
    self.browser.maximize_window()

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

def test_admin_site(self):    
    # user opens web browser, navigates to admin page
    self.browser.get(self.live_server_url + '/admin/')
    body = self.browser.find_element_by_tag_name('body')
    self.assertIn('Django administration', body.text)
    # users types in username and passwords and presses enter
    username_field = self.browser.find_element_by_name('username')
    username_field.send_keys('admin')
    password_field = self.browser.find_element_by_name('password')
    password_field.send_keys('admin')
    password_field.send_keys(Keys.RETURN)
    time.sleep(3)
    # login credentials are correct, and the user is redirected to the main admin page
    body = self.browser.find_element_by_tag_name('body')
    self.assertIn('Site administration', body.text)

The test fails with error message:

AssertionError: 'Site administration' not found in u'Django administration\nPlease enter the correct username and password for a staff account. Note that both fields may be case-sensitive.\nUsername:\nPassword:\n '

PS: I use Postgres for backend database. All the tutorials I see uses 'sqlite3'

Это было полезно?

Решение

You're starting from a fresh test database so your admin user does not exist yet. You need to create it manually in the test case.

This should work:

from django.contrib.auth.models import User
User.objects.create_superuser('admin', 'admin@mysite.com', 'admin')
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top