Equipped with the web application structure, the user stories and the design sketches we will now start creating the tukker app. This time we will use a different approach called Test-Driven-Development.
Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new code to acceptable standards.
In TDD you first write a test, that has to fail and then write the production code, that passes the test. For example with our home page we will write a test that checks if there is a "Register Now!" link an if it's clickable. This way we simulate an user that comes to our home page.
Why Test-Driven Development
Imagine, you develop the app for John based on the user stories we created. You develop the code for the static pages, then John tests this pages. He shows you a few errors, you fix them. After that you begin with the rest of the web application, the new code affects the old one, errors creep in. Next time John tests the tukker app he an sees new errors on the static pages. He tells you again - a bit disappointed. You wonder: "But I didn't made any changes to the static pages."
TDD helps you to notice these errors and to be sure that your code is working. With tests you are free to optimize your code or to add new functionality - your test will let you know if something went wrong.
Writing the first test
Before we are writing our first test, we need to create our tukker app. Open a terminal and go to your web2py installation (this should seem familiar to you):
$ cd /your/web2py/folder # on Windows use backslashes
again add the following code to
.hgignore, save and close gedit:
of course you can copy the
.hgignore file from the pitch application; back in the terminal initialize the Mercurial repository:
$ hg init
Push the repository to BitBucket (optional)
Again you can push your repository to BitBucket, this is optional, but think about it, it's a free backup of your code.
Create a new repository called tukker or anything you like best. In the terminal:
$ hg push https://email@example.com/your_username/tukker
Install the selenium testing framework
Now that we created the tukker, we will first setup our testing environment with Selenium. Selenium allows us to tell Firefox to behave like a real user - it's web browsing on autopilot.
In the terminal go to your application directory (On Windows you should probably download the package in your web browser and extract it to the
lib directory and rename the directory to
selenium, be sure to have a program that can handle tar files.):
$ cd /your/web2py/folder/applications/tukker # on Windows use backslashes
now we need to create two files:
__init__.py in the fts directory,
functional_tests.py in the tukker application base directory. Let's fire up gedit:
$ gedit __init__.py
in the gedit windows now press
Ctrl+S to save the empty file.
__init__.py tells the Python interpreter that the directory
fts is a package, this is important for our next file
functional_tests.py. In the gedit window press
Ctrl+N, after the new tab gets created fill in the following content - this file runs our tests for us, don't worry if you don't understand this:
Test if Selenium works as exspected
Its time to test drive our Selenium setup to check if everything is working as we exspect it. Create a new file
test_home.py in the
$ cd fts
fill in the following content:
from functional_tests import FunctionalTest, ROOT
save the file.
What does this test file do? In line 8 we open the URL
http://localhost:8001/tukker/, then in line 11 und 12 we look for the text
Messages With 300 Chars in the body of the webpage.
Let's see if the our tukker app will pass this test? In the terminal start the functional tests:
$ python functional_tests.py
a Firefox window should appear and you should see the following output in the terminal:
of course the test fails, as we exspected and wanted it. It seems our testing framework is working; let's commit the changes we made to the repository:
$ hg add fts/*
Fix our first test
Ok now we know that our testing framework is working, we need to fix our first test.
Think about it. How would you fix this? What do we know?
- What do we need to change? Some text in the body of the home page needs to be "Messages With 300 Chars"
- Where do we find the files for fixing this? Remember the Model-View-Controller? The controller for the home page was the function 'index' in the file 'default.py', the corresponding view was
We need the include the text in our view. So open it and change the headline:
$ gedit views/default/index.html
search for the line that starts with
<h4> and change the text in this line to
Messages With 300 Chars:
now let's check if we pass our functional test:
$ python functional_tests.py
We can see that 1 test was run, later we will run much more tests. If you see the
Ok at the end of the terminal we passed our first test. Congrats!.
You've taken an important step in becoming a serious programmer.
Again let's commit our changes and then push our repository to BitBucket that we have a backup if we need one:
$ hg commit -m"Passed first functional test"