Package: cypress-django
cypress-djangoPython and Node.js package providing support for Cypress and Django integration.Python: Cypress DBIssue commands to operate on the project's Cypress test database.Expectssettings/cypress.pyto exist for the Django settings andcypress/db/setup_test_data.pyto exist to house the functions for loading
in test data (though both can be customised, see below).This script can be used in Cypress tests to load data into the test database,
as well as be run on the command line as a shortcut for various operations on the
test database.It caches the last loaded test data function and exits early if the same function
is loaded again - for tests that do not alter the database, it is not necessary to
reload the data, so we can save time here.Implemented as a standalone script rather than a management command to avoid the
overhead ofmanage.pywhen exiting early, since we want tests to be as fast as
possible.Installationpip install cypress-djangoUsagecypress_db [-h] [--init] [--flush] [--clearcache] [func]positional arguments:func          Setup test data function to runoptional arguments:-h, --help    show this help message and exit
  --init        Initialise the database (run `migrate` and `createcachetable`)
  --flush       Clear all data (run `flush`)
  --clearcache  Delete the test data cache (use when a test will modify the database)Setup test data functionsThese are python functions that should load data into the test database as required for
tests. The functions should not have any required arguments, as when they are invoked,
no arguments will be passed. There is no particular restriction on what the functions
can do, so it is possbile to have helper functions setup to do common setup that the
exposed setup functions can call.For example, one possible function could be to create a superuser:defsuperuser():User.objects.create_superuser(username="test",password="a")Another function may be something like:defmake_some_objects():# we also need a create a superusersuperuser()# make some objects# ...When a new function is loaded, the database is always flushed first, so you are starting
from scratch every time.ConfigurationEnvironment variables:CYPRESS_SETTINGS- python module for the cypress settings (default<project_name>.settings.cypress)Settings:CYPRESS_SETUP_TEST_DATA_MODULE- python module for setup test data functions (defaultcypress.db.setup_test_data)CYPRESS_CACHE_KEY- cache key for tracking last setup test data function loaded (defaultcypress_last_func)CYPRESS_CACHE_TIMEOUT- how long theCYPRESS_CACHE_KEYlasts before expiring in seconds (default 1 day)Node.js: Cypress CommandsProvides aloginhelper function:cy.login()will programmatically login using theUSERNAMEandPASSWORDenvironment
variables defined incypress.json(or withCYPRESS_prefix if defined elsewhere)Recommended way to use is to putcy.login()inbeforeEachIf it is necessary to login as a different user, for example to test behaviour for users with
limited permissions, simply provide the appropriate username and password as argumentsProvides asetupDBhelper function that expects the python package in this repo to be
installed:cy.setupDBwill flush the test database and load new data according to the
functionsetupFuncIf the test will write to the database,mutableshould be set totrueOtherwise setmutabletofalseto allow early exit from the script if no DB setup
is necessary (this means repeated test runs with such tests will be significantly faster)ThesetupFuncargument should be the name of a function living incypress/db/setup_test_data.pywhich loads whatever data necessary into the test database - this is similar to aTestCase.setUpTestDatamethodExamplecypress.json:{"baseUrl":"http://127.0.0.1:8000","env":{"USERNAME":"test","PASSWORD":"a"},"viewportHeight":800,"viewportWidth":1400}InstallationEnsurecypressis installed:npm install cypress --save-devInstallcypress-django:npm install git+https://github.com/QuickRelease/cypress-django.git --save-devand include this line incypress/support/index.jsorcypress/support/commands.js:import 'cypress-django/commands'