[Answered ]-How do I set up a Django user for use in a Selenium test case with the setUp method?

1👍

When you run tests using Django’s test framework (like using LiveServerTestCase), django sets up a separate test database for the duration of the tests.
This test database is independent of your project’s main database.

The setUp method in your test class correctly creates a user in this test database, but if your Selenium test is running against your main application (which is using your project’s main database), it won’t have access to the user created in the test database. That’s why your login attempts fail until you create a user manually in the main database.

A few options:

  • Use Django’s Test Server: The URL in your test method self.driver.get('http://127.0.0.1:8000/login') should point to the test server, which usually runs on a different port. Django’s LiveServerTestCase provides a live_server_url attribute, which gives you the URL of the test server.

    self.driver.get(self.live_server_url + '/login')
    
  • Create a User in Your Test Setup and use it

👤Yuri R

Leave a comment