This guide will help you create and run test cases using Selenium in conjunction with Django's testing tools. Follow these steps to set up your test environment and generate the necessary data.
Generally test cases should be written for the following to reasons mainly:
- To test a part of the website that has broken frequently. This is called regression testing.
- To test most critical aspects of website which if broken could be costly for the buisness of the website.
Before writing your test cases, ensure that all the relevant data is added to the Django admin. This will be the data your test cases will interact with.
Once you have added the necessary data in the admin, export the database to a JSON file.
python manage.py dumpdata > test_database_name.json
Add the database to the fixtures
folder in tests_ubyssey
directory. Then you can start the static live server that Django provides which runs in the
background and feed it the database you created as a fixture. Then you can do the test setup similar to what is done in tests.py
file in tests_ubyssey
directory. For more details kindly refer to the Django testing documentation and selenium
documentation for detailed instructions on creating test cases.
To ensure that the parameterized tests are working as expected, follow these steps:
-
Rebuild Docker Compose:
- First, rebuild your Docker Compose configuration to reflect the three new containers added to the
ubyssey-dev
repository.
- First, rebuild your Docker Compose configuration to reflect the three new containers added to the
-
Run this command:
# Go to ubyssey.ca directory
pip install -r requirements.txt
- Collect static files
- If you don't have a directory
ubyssey.ca/static
, then navigate toubyssey.ca/config/settings/base.py
and uncomment out the line'whitenoise.runserver_nostatic'
inINSTALLED_APPS
. Then run the commandpython manage.py collectstatic
in theubyssey.ca
directory in terminal. This will create the directoryubyssey.ca/static
and populate it with the project's static files. These static files have to exist in order to run the tests because the test server is run withDEBUG=False
which changes where the static files are expected to be.
- If you don't have a directory
-
Start the Chrome Container:
- In Docker Desktop, start the
selenium/standalone-chrome:latest
container.
- In Docker Desktop, start the
-
Run the Tests:
- Set the browser environment variable to Chrome and run the tests:
# Go to the ubyssey.ca dir export BROWSER=chrome && python manage.py test
- Set the browser environment variable to Chrome and run the tests:
-
Start the Firefox Container:
- In Docker Desktop, start the
selenium/standalone-firefox:latest
container.
- In Docker Desktop, start the
-
Run the Tests:
- Set the browser environment variable to Firefox and run the tests:
# Go to the ubyssey.ca dir export BROWSER=firefox && python manage.py test
- Set the browser environment variable to Firefox and run the tests:
-
Start the Edge Container:
- In Docker Desktop, start the
selenium/standalone-edge:latest
container.
- In Docker Desktop, start the
-
Run the Tests:
- Set the browser environment variable to Edge and run the tests:
# Go to the ubyssey.ca dir export BROWSER=edge && python manage.py test
- Set the browser environment variable to Edge and run the tests:
By following these steps, you will be able to verify that the parameterized tests work correctly across different browsers.