enewe101 / digidemo

digital democracy engine
1 stars 0 forks source link

Add a test case for creating new Discussions #42

Closed enewe101 closed 9 years ago

enewe101 commented 9 years ago

We currently don't have any test case for the addition of new Discussions. Ideally, every aspect of functionality should have an associated test case. This doesn't always happen, but it's essential to add a test case after finding a bug (I just did, see issue #41 ), so that the bug can never come back (which is something bugs like to do).

When you write your first test case, it may seem a bit pointless. Often it is the case that you have the system behaving the way you want it to, and your test simply confirms that it's doing what it's already doing. But there is a very good reason for writing the test: it's very common to start changing something in the app, only to break something we had built before. If we have test cases protecting existing functionality, we'll be warned as soon as we break something, and that means we can fix the bug immediately and easily. On the other hand, without tests, we risk pushing the bug onto the production server, only to discover it much later, when it is difficult to determine what change we made that caused the bug!

So this issue is to create a test case!

First, do git pull to make sure you're up to date (you should receive a new branch called discussion_test. Now checkout that branch, git checkout discussion_test.

Now, have a look in the file test.py -- that's where we keep our tests. Scroll down until you find the class AddDiscussionTest. I've partly created the test already -- the basic outline is there, all you need to do is fill in some of the details. Have a look at how this is working. Notice that the test is defined inside a python class, and that it inherits from SeleniumTestCase.

SeleniumTestCase is a kind of test case that I defined which ensures that your test communicates with a test server instance -- a server that is created, just like the one you instantiate when you do python manage.py runserver. It also makes your test case able to open a browser and simulate user behavior, using the self.driver attribute.

Inside the test case, there are two test methods -- test_add_complete_discussion, and test_add_incomplete_discussion. You'll need to fill in some details to make these tests work.

For the first test, you mainly just need to find some html id's that help the test know where to look on the webpage. The test uses these id's to A) find form inputs so that it can input a new discussion using the form (just like a user would), and B) find certain displayed elements after submitting the Discussion, to make sure that the discussion is displaying properly.

You'll need to write more yourself in the second test. This one tests what happens when a discussion form is filled out improperly (i.e. one of the fields is left blank).

To test your test (hehe) you go to the src directory, and do

python manage.py test digidemo.test.AddDiscussionTest

That will run just your test (otherwise you'll be waiting a couple minutes for all the other tests to run.

Of course, after completing your test, you should run the full test suite, something you should always do when ready to close an issue!

python manage.py test
mlhuish commented 9 years ago

Closing this issue woo!