วันพฤหัสบดีที่ 29 มกราคม พ.ศ. 2558

Chapter 3 : Testing a Simple Home Page with Unit Tests

"Our First Django App, and Our First Unit Test"

First time use command in "superlists" directory from Chapter 2



and it create a folder at superlists/lists
 
     superlists/
     ├── db.sqlite3
     ├── functional_tests.py
     ├── lists
     │   ├── admin.py
     │   ├── __init__.py
     │   ├── migrations
     │   │   └── __init__.py
     │   ├── models.py
     │   ├── tests.py
     │   └── views.py
     ├── manage.py
     └── superlists
         ├── __init__.py
         ├── __pycache__
         ├── settings.py
         ├── urls.py
         └── wsgi.py
 
and next Unit Testing in Django , Open up the new file at lists/tests.py then edit to this code 


and now let's to run manage.py with this command


Output Result

Then commit this git with this commands


and next , Django’s MVC, URLs, and View Functions
in Django’s workflow goes something like this:
  1. An HTTP request comes in for a particular URL.
  2. Django uses some rules to decide which view function should deal with the request (this is referred to as resolving the URL).
  3. The view function processes the request and returns an HTTP response
So we want to test two things:
  • Can we resolve the URL for the root of the site (“/”) to a particular view function we’ve made?
  • Can we make this view function return some HTML which will get the functional test to pass? 
At first Open up lists/tests.py, and change to something like this:



Output Result


" ImportError: cannot import name 'home_page' " 

Solution : We can’t import home_page from lists.views? OK, let’s fix that—and only that. In lists/views.py:


Let’s run the tests again with this command:


Output Result


 next , Django uses a file called urls.py to define how URLs map to view functions. and change it to same like this:


Then , Run the unit tests again, with


Output Result

the dot-notation superlists.views.home doesn’t point to a real view. Let’s fix that, by pointing it towards our placeholder home_page object, which is inside lists, not superlists (file : superlists/urls.py)
 

And run the tests again: Output Result

The unit tests have made the link between the URL / and the home_page = None in lists/views.py, and are now complaining that home_page isn’t a callable; ie, it’s not a function. Now we’ve got a justification for changing it from being None to being an actual function. Every single code change is driven by the tests. Back in lists/views.py:


and the last run with command :

Output Result


and TIME TO GIT


NEXT ,Unit Testing a View
Open up lists/tests.py, and add a new test method.


Let’s run the unit tests now and see how we get on ... with command :

Output Result

" TypeError: home_page() takes 0 positional arguments but 1 was given "

now,Change code in lists/views.py
 

Tests again: Output Result
" self.assertTrue(response.content.startswith(b'<html>')) AssertionError: False is not true "

Coding Again Fix return area:

 
Tests Again : Output Result


" AssertionError: b'<title>To-Do lists</title>' not found in b'<html>' "

AND Coding Again Fix return area:

 
Tests Again : Output Result

And Time to GIT


and the last : try to typing git log



Summary

  Useful Commands and Concepts

-----END-----
credit  :  http://chimera.labs.oreilly.com/books/1234000000754/ch03.html#_urls_py

ไม่มีความคิดเห็น:

แสดงความคิดเห็น