Skip to main content

Helpers for creating functional tests in Django, with a unified API for WebTest and Selenium tests.

Project description

https://github.com/django-functest/django-functest/workflows/Python%20package/badge.svg https://readthedocs.org/projects/django-functest/badge/?version=latest

Helpers for creating high-level functional tests in Django, with a unified API for WebTest and Selenium tests.

Exploring django-functest makes me angry! Why? Because I’ve wasted so much time writing low-level, boilerplate-filled tests for the past few years instead of using it — jerivas

What is WebTest? Imagine a text-based, HTML-only browser that doesn’t load CSS, Javascript etc, operates directly on a WSGI interface in a synchronous fashion for performance and robustness, and is controlled programmatically.

What is Selenium? A tool that opens full browsers like Firefox and Chrome (with an isolated profile), and provides an API for controlling them.

For an idea of what writing tests with django-functest looks like in practice, you might be interested in the video in our writing tests interactively documentation.

Documentation

The full documentation is at https://django-functest.readthedocs.org.

Installation

Python 3.7 and later, Django 2.0 and later are required.

pip install django-functest

See also the dependencies documentation for important compatibility information.

Features

  • A simplified API for writing functional tests in Django (tests that check the behaviour of entire views, or sets of views, e.g. a checkout process).

  • A unified API that abstracts over both WebTest and Selenium - write two tests at once!

  • Many of the gotchas and difficulties of using WebTest and Selenium ironed out for you.

  • Well tested - as well as its own test suite, which is run against Firefox and Chrome, it is also used by Wolf & Badger for tests covering many business critical functionalities.

  • Supports running with pytest (using pytest-django) as well as Django’s manage.py test

Typical usage

In your tests.py:

from django.test import LiveServerTestCase, TestCase
from django_functest import FuncWebTestMixin, FuncSeleniumMixin, FuncBaseMixin


class ContactTestBase(FuncBaseMixin):
    # Abstract class, doesn't inherit from TestCase

    def test_contact_form(self):
        self.get_url("contact_form")
        self.fill(
            {
                "#id_name": "Joe",
                "#id_message": "Hello",
            }
        )
        self.submit("input[type=submit]")
        self.assertTextPresent("Thanks for your message")


class ContactWebTest(ContactTestBase, FuncWebTestMixin, TestCase):
    pass


class ContactSeleniumTest(ContactTestBase, FuncSeleniumMixin, LiveServerTestCase):
    pass

In this way, you can write a single test with a high-level API, and run it in two ways - using a fast, WSGI-based method which emulates typical HTTP usage of a browser, and using a full browser that actually executes Javascript (if present) etc.

The approach taken by django-functest is ideal if your web app is mostly a “classic” app with server-side rendered HTML combined with a careful sprinkling of Javascript to enhance the UI, which you also need to be able to test. If such an approach seems old-fashioned to you, have a look at htmx.org or hotwire and get with the new kids! (OK most of are actually quite old but we make fast web sites…)

Under the hood, the WSGI-based method uses and builds upon WebTest and django-webtest.

django-functest provides its functionality as mixins, so that you can have your own base class for tests.

Contributing and tests

See CONTRIBUTING.rst for information about running the test suite and contributing to django-functest.

Building on Mac OS

While this lxml bug is in effect lxml cannot handle certain unicode characters in HTML (or XML!) documents on Mac OS, including the emoji used in one of the files in the test suite.

Therefore, if you are affected by this bug, you will find that certain tests fail with the error lxml.etree.ParserError: Document is empty.

You will also find that lxml’s own test suite fails on your machine.

A workaround is to compile libxml2 yourself, which lxml will take care of for you. To do this, run the following:

` STATICBUILD=true python -m pip install lxml --force-reinstall --no-binary=:all: `

Credits

This library was written originally by spookylukey, further improved by developers at Wolf & Badger, and released with the kind permission of that company.

Tools used in rendering this package:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-functest-1.6.1.tar.gz (78.5 kB view details)

Uploaded Source

Built Distribution

django_functest-1.6.1-py3-none-any.whl (40.6 kB view details)

Uploaded Python 3

File details

Details for the file django-functest-1.6.1.tar.gz.

File metadata

  • Download URL: django-functest-1.6.1.tar.gz
  • Upload date:
  • Size: 78.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for django-functest-1.6.1.tar.gz
Algorithm Hash digest
SHA256 baa02f9ec3b45eea6b8a37eb224605fb8019c4a125d1a271e21698faed6ab6ed
MD5 e56c46f3d042eb0648236aeb3e752225
BLAKE2b-256 9fd14314f2cee6c67f3daed66e02826a9ff581b475d751f996fca771cdf34222

See more details on using hashes here.

File details

Details for the file django_functest-1.6.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_functest-1.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d4608f2aabca329270054104d87499870c559cd6bdd418969190daffab179f7c
MD5 52a2d1c186169e5ed830c9f15fdd5e08
BLAKE2b-256 8924121305c7bf648532b9c33aaeb6eb6de4a112162a8b3f9e86685cf60c4607

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page