Skip to content

Instantly share code, notes, and snippets.

@bchewy
Last active November 13, 2019 08:30
Show Gist options
  • Save bchewy/0e67a6d7dbe11732ce13a3daf47912be to your computer and use it in GitHub Desktop.
Save bchewy/0e67a6d7dbe11732ce13a3daf47912be to your computer and use it in GitHub Desktop.
writing selenium test cases with pytest on Django.

Writing selenium test cases with pytest on Django.

This document is not written in any particular order. It is ordered in sections

  • Tips for using the Selenium WebDriver
  • Using the pytest command on the CLI
  • Configurating Pytest for Django

Follow at your own risk!

Tips for using the Selenium WebDriver

  1. Make sure to use driver methods to see where you're at! eg: driver.page_source tells you what page you're on!
  2. Make sure test cases are proper - you can check if an element exists; and more!

Using the pytest command on the CLI

In this section you'll find different ways you can use the pytest command on CLI; most notably i found these to be helpful. (http://doc.pytest.org/en/latest/usage.html)
pytest -v --cov - this throws verbose and coverage
pytest -v --cov=<projectname> this throws verbose and coverage for specific project

Otherwise, you can also set up configuration files - so you don't have to type in pytest with long flags each time.

Configurating Pytest for Django

PS: Not required for CA.
You'll need the two plugins for pytest

  1. pytest-django
  2. coverage.py plugin (is installed by default - I believe!)

Step 1. Create two files in the root of your project directory (./pytest.ini & ./.coveragerc)
Step 2. Configure the two files in the following manner:
pytest.ini (http://doc.pytest.org/en/latest/customize.html)

[pytest]
python_files = tests.py test_*.py *_tests.py
DJANGO_SETTINGS_MODULE = personal_portfolio.settings
addopts = --cov=. --cov-report html --cov-config=.coveragerc 

.coveragerc (https://coverage.readthedocs.io/en/latest/config.html)

[run]
omit = */manage.py, */wsgi.py, */urls.py, */views.py, */forms.py
branch = true
source = .

[report]
show_missing = false

Most importantly, check that the two files are coupled with the --cov-config=.coveragerc flag.
Step 3. You can run pytest with just the pytest -v now and it should run it with all the addopts.

Using Pytest with Django (without pytest-django)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment