Background Image
Table of Contents Table of Contents
Previous Page  370 / 478 Next Page
Information
Show Menu
Previous Page 370 / 478 Next Page
Page Background

form

=

NewListForm

(

data

=

request

.

POST

)

if

form

.

is_valid

():

list_

=

form

.

save

(

owner

=

request

.

user

)

# creates both List and Item

return

redirect

(

list_

)

else

:

return

render

(

request

,

'home.html'

, {

"form"

:

form

})

That would be neater! Let’s see how we’d get to that state by using fully isolated tests.

Rewriting Our Tests for the View to Be Fully Isolated

Our first attempt at a test suite is for this view was highly

integrated

. It needed the

database layer and the forms layer to be fully functional in order for it to pass. We’ve

started trying to make it more isolated, let’s now go all the way.

Keep the Old Integrated Test Suite Around as a Sanity Check

Let’s rename our old

NewListTest

class to

NewListViewIntegratedTest

, and throw

away our attempt at a mocky test for saving the owner, puttting back the integrated

version, with a skip on it for now:

lists/tests/test_views.py (ch19l008).

import

unittest

[

...

]

class

NewListViewIntegratedTest

(

TestCase

):

def

test_saving_a_POST_request

(

self

):

[

...

]

@unittest.skip

def

test_list_owner_is_saved_if_user_is_authenticated

(

self

):

request

=

HttpRequest

()

request

.

user

=

User

.

objects

.

create

(

email

=

'a@b.com

'

)

request

.

POST

[

'text'

]

=

'new list item'

new_list

(

request

)

list_

=

List

.

objects

.

first

()

self

.

assertEqual

(

list_

.

owner

,

request

.

user

)

Have you heard the term “integration test” and are wondering what

the difference is with an “integrated test”? Go and take a peek at the

definitions box in

Chapter 22

.

$

python3 manage.py test lists

[...]

Ran 37 tests in 0.139s

OK

342

|

Chapter 19: Test Isolation, and “Listening to Your Tests”