Integrate QUnit JavaScript tests into a Django test suite via nose
Project description
Integrate QUnit JavaScript tests into a Django test suite via nose.
Installation
pip install django-nose-qunit.
Add 'django_nose_qunit' to your INSTALLED_APPS setting.
Ensure that you’re using nose as your test runner by using the following Django setting
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
Enable the nose plugin by adding it to the NOSE_PLUGINS Django setting:
NOSE_PLUGINS = [ 'django_nose_qunit.QUnitPlugin' ]
and the NOSE_ARGS Django setting:
NOSE_ARGS = [ '--with-django-qunit', ]
Add an entry to your URL configuration:
from django_nose_qunit.urls import urlpatterns as qunit_urlpatterns urlpatterns += qunit_urlpatterns
This adds new URLs of the form /qunit/*, and they return a 404 unless DEBUG is True or QUnit tests have been initialized as part of a test run.
Configure Selenium as explained in the sbo-selenium README.
Make sure MEDIA_URL is set to some non-empty string, like “/media/”. If this is not done, the live test server can occasionally get confused and treat requests for a test page as requests for static files.
Creating Unit Tests
Tests can be written in JavaScript using QUnit as normal; see the QUnit documentation for details. You need only create a JavaScript file, not the HTML page that will load it (that is provided by the template at qunit/template.html). If your tests depend on HTML fixtures in the qunit-fixture div, create those as HTML fragments in files which can be loaded as templates. External script dependencies should be files in the staticfiles load path. You should add QUnit.Django.start(); before your test definitions and QUnit.Django.end(); at the end of your test definitions; this allows the tests to start executing at an appropriate time depending on whether they’re running in a browser, in a nose test run, or inside a require() block of an AMD loader like RequireJS.
To make nose aware of your QUnit tests, create a subclass of django_nose_qunit.QUnitTestCase in a file which would normally be searched by nose, for example my_app/test/qunit/test_case.py. It can contain as little as just the test_file attribute (a path to a QUnit test script, relative to STATIC_URL). Any script dependencies for your test script should be given as paths relative to STATIC_URL in the dependencies attribute. Paths to HTML fixture templates are listed in the html_fixtures attribute.
Running Unit Tests
Use your normal test execution command (using django-admin.py or manage.py). Execution can be restricted to one or more specified packages and/or classes as normal (“myapp”, “myapp.tests.qunit”, “myapp.tests.qunit:MyTestCase”, etc.). There is currently no support for running only a single module or test within a QUnit test script; QUnit module and test names can be arbitrary strings, which makes it difficult for the nose command line parser to handle them.
To run the QUnit tests in a regular web browser, use the runserver management command with QUNIT_DYNAMIC_REGISTRY set to True (by default, it has the same value as DEBUG). If DEBUG is False, you’ll also need to use the --insecure parameter to serve static files. You can then access a list of links to the available QUnit tests at a URL like http://localhost:8000/qunit/. This can be useful when first developing a test script and when troubleshooting failing tests.
How It Works
QUnitTestCase is a subclass of Django’s LiveServerTestCase, which starts a Django test server in the background on setup of the test class and stops it on teardown. django_nose_qunit includes a nose plugin which can accommodate tests written as simple wrappers for JavaScript test files. When nose searches for tests to run, the plugin tells it how to ask a browser via Selenium WebDriver to load each test script (without running the tests) in order to get information about the modules and tests it contains. Once these tests are enumerated, they are run like any other test case. The first execution of a test from a QUnit test script runs all of the tests in the script, and the results are stored. Each test case then reports success or failure based on the reported results, with failures including any messages provided by QUnit.
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
Built Distribution
File details
Details for the file django-nose-qunit-1.6.1.tar.gz
.
File metadata
- Download URL: django-nose-qunit-1.6.1.tar.gz
- Upload date:
- Size: 44.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5442c11eae4d292ce3ae07769c9cf8840fe034de72b942246acc4c0213f65cd |
|
MD5 | 97c8da637b0ea02d77c80ac3f052b29d |
|
BLAKE2b-256 | 25a9072577a11d1d4e067b6935ae9577c33835cb55bcf4530f3cce0fbaae2a9d |
File details
Details for the file django_nose_qunit-1.6.1-py2.py3-none-any.whl
.
File metadata
- Download URL: django_nose_qunit-1.6.1-py2.py3-none-any.whl
- Upload date:
- Size: 49.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae8586d92f4138fd7d61dda58af0a5e13f286d4ce0f2fc65eae7b49d64b82618 |
|
MD5 | 0df264aea492efaeb25e6cd4a9fb9c04 |
|
BLAKE2b-256 | 280c83989ed40061e1cc4190ce3403887eb36ae0d1d4ae4a847bb0afe3e45308 |