Notes to Test driven development with Django using pycharm (chp 2-3)
Contents
0 Checkpoints
- Blackbox Test=Functional Test=Acceptance Test= End-to-End Test ; These names are referring to the same thing. The purpose of this kind of test is to look at how the whole application functioned from outside/custom’s point of view. The term black box test is forged is because we just want to test the outcome of the whole code without knowing the details inside
App = function; Structure the code into several apps/ functions (def function()). In this way, the whole program is build up like Lego’s toy. You could easily use other’s Lego block or even the one you used before.The advantages are many, but one of them could tell you that they just save monk.
Coming up with a “user story”, to help with function design and developmental flow as well as the interface design.
Unit test library from Django library
Get started with a minimum possible app
Further development by unit-test \=\=> coding \=\=> unit test \=\=> coding … cycle
1 Get started with an app of minimum possible function
The minimum function is actually “after started, serve an empty webpage.” We already did this in chp1, but the word “it works” and the title “Django” are from the Django. However, here we want to give out the message ourselves.
- Remember, this is called test driven development, so we should first write the test and then write the code of the function you want to add to the program. Then keep revising it until the code passes the test. However, the problem is, sometime you focused on the test but lost the big picture of how software is going to evolve. Well, that’s where the user/customer’s story get in. The user’s story will help you to find out what’s the next step you should take.
Here, I re-organize the logic flow of the book. I first go to chp3 to create a new app by copying the “template” and then back to chp2 to construct functional test then back to chp3 again to construct the unit test. Although this would make no “test-driven” feeling, it put the two tests side by side and convenient for first time user to compare them.
To start a new app, we need to change the parameter in command line to “startapp lists”. The “startapp” is the parameter that ask manage.py to copy out template to a new app with the name “lists”. You could edit the configuration of running mange.py from run menu or use command line:
|
|
from django.test import TestCase class SmokeTest(TestCase): def test_bad_maths(self): self.assertEqual(1 + 1, 3)
|
|
python manage.py test
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The above code , the extra code compared to the last version to be precise, simulates a browser to send a HTTP request to “/” and test whether the response it returns would be the same as several assertions assumed.
You would now have trouble run the unit test. (Remember the way to run unit test is to give “test” as command line parameter to manage.py)
Now we demonstrate the test-driven method. Or here is unit-test \=\=> coding \=\=> another unit-test \=\=> coding … cycle
The first error you got is you TypeError, telling you that home_page() takes 0 positional arguments but 1 was given.
- This is easy to fix, we change the home_page() function to accept argument:
|
|
The second error you got should be an “assertion error” which make sense, since we didn’t give any contents to home_page (only a “pass” ). So it couldn’t match any assertion command.
- This is fixed by:
|
|
The third error, is “False is not true”. When you see this, you should know we are close. This means, the Django now returns something. However, It is not return something it suppose to.
- fix:
|
|
|
|
- The next two “hotfixs” are basically to solve the rest of the assertion in the unit test.
|
|
We don’t have to write three separate HttpResponse(), because one would contains all of the elements that assertions are looking for. However, it doesn’t hurt that you could try to write three separate HttpResponse() with each correponds to one assertion.
6 Push the codes to github.com
After finishing a basic code, we push all code to github.com which helps in version control.
Additionally, when asked to provide a name for the new git warehouse, on the same webpage, you could find a small checkbox on the bottom to let you choose to create READMT.md. Don’t choose this! Just create an empty git warehouse. You can add “Readme.md” later. Or you could have a non-forward error. And solution is the do a “fetch” or “Pull+Merge” but why not do it easier.
|
|