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

1. Admittedly once you start looking for Python BDD tools, things are a little more confusing.

# She is pleased to see that the error message disappears

error

=

self

.

browser

.

find_element_by_css_selector

(

'.has-error'

)

self

.

assertFalse

(

error

.

is_displayed

())

#

is_displayed()

tells you whether an element is visible or not. We can’t just rely

on checking whether the element is present in the DOM, because now we’re

starting to hide elements.

That fails appropriately, but before we move on: three strikes and refactor! We’ve got

several places where we find the error element using CSS. Let’s move it to a helper

function:

functional_tests/test_list_item_validation.py (ch14l002).

def

get_error_element

(

self

):

return

self

.

browser

.

find_element_by_css_selector

(

'.has-error'

)

I like to keep helper functions in the FT class that’s using them, and

only promote them to the base class when they’re actually needed

elsewhere. It stops the base class from getting too cluttered. YAGNI.

And we then make five replacements in

test_list_item_validation

, like this one for

example:

functional_tests/test_list_item_validation.py (ch14l003).

# She is pleased to see that the error message disappears

error

=

self

.

get_error_element

()

self

.

assertFalse

(

error

.

is_displayed

())

We have an expected failure:

$

python3 manage.py test functional_tests.test_list_item_validation

[...]

self.assertFalse(error.is_displayed())

AssertionError: True is not false

And we can commit this as the first cut of our FT.

Setting Up a Basic JavaScript Test Runner

Choosing your testing tools in the Python and Django world is fairly straightforward.

The standard library

unittest

module is perfectly adequate, and the Django test runner

alsomakes a gooddefault choice. There are some alternatives out there

nose

is popular,

and I’ve personally found

pytest

to be very impressive. But there is a clear default option,

and it’s just fine.

1

226

|

Chapter 13: Dipping Our Toes, Very Tentatively, into JavaScript