Open enewe101 opened 10 years ago
Ideally, these tests should be written in such a way that they would run on a local machine, issuing http requests to a test deployment server over the Internet. This will give us a suite of tests that truly tests the full stack, including the network dependency.
_The following is a good issue for new developers. It will introduce you to the testing framework, the urlresolver, and python inheritance.
Intro
The Selenium WebDriver software allows us to run tests of the site through a Firefox browser, by simulating user interactions. This means that we can run tests which initiate a request from a browser, and then verify that the content sent back to the browser is correct, i.e. it allows us to run "end-to-end" tests.
This issue is to create a suite of tests that verify the rendering of all of the views. The purpose with these tests is not to verify details of form interaction or the consistency of business logic, but rather, simply to verify that each page displays as expected.
Task
A dedicated test class called
RenderTest
should be created inside~/test.py
for this purpose.RenderTest
should extendSeleniumTestCase
.Write a method in
RenderTest
calledvalidate_render()
. This method should accept one argument consisting of a list calledrender_specs
, which contains a series ofdict
s, each called arender_spec
. Eachrender_spec
should contain the keyselement_id
, andcontent
. The methodvalidate_render()
should go through the list of render_specs and use Selenium to verify that each element specified in theelement_id
exists, and containscontent
. It should be allowed thatcontent
can beNone
, in which case,render_test()
will only verify that the elementelement_id
exists, and won't check its content. Ifcontent
is the empty string, that should be tested as an assertion that the element content actually is the empty string.Next, make methods to test every view listed in urls.py. There should be one test method per view. These methods should be named like this:
test_<view-name>
. You can tell what the<view-name>
of a view is by looking at the third argument passed into url() in the urls list. Note that, inside an instance ofTestCase
, methods whose name starts withtest_
will get discovered and executes as a tests when the commandpython manage.py test
is issued in~/src
.Each test method should do this:
It's not necessary to test a views handling of POST requests (i.e. you do not need to validate form submission -- we have other tests for that).
Tip
Some views will require that the user logs in first. Since it will be common to need to login, create a method called
login()
which fills out the login form and submits it. You can login using the testing userregularuser
whose password isregularuser
. Since logging in will potentially be needed by any test using Selenium, put this method at a higher point in the inheretance structure, as a method for SeleniumTestCase.Tip
While writing your tests, you'll find it convinient to be able to run only that test, rather than the whole suite. You can do that by issuing the command
python manage.py test digidemo.test.<TestCaseName>.<test_method_name>