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

We also need a way of resetting the server database in between each test. I’ll

explain the logic of the session-creation code, which should also explain how

this works.

An Additional Hop via subprocess

Because our tests are Python 3, we can’t directly call our Fabric functions, which are

Python 2. Instead, we have to do an extra hop and call the

fab

command as a new

process, like we do from the command line when we do server deploys. Here’s how that

looks, in a module called

server_tools

:

functional_tests/server_tools.py.

from

os

import

path

import

subprocess

THIS_FOLDER

=

path

.

abspath

(

path

.

dirname

(

__file__

))

def

create_session_on_server

(

host

,

email

):

return

subprocess

.

check_output

(

[

'fab'

,

'create_session_on

_server:email=

{}'

.

format

(

email

),

#

'--host={}'

.

format

(

host

),

'--hide=everything,status'

,

#

],

cwd

=

THIS_FOLDER

)

.

decode

()

.

strip

()

#

def

reset_database

(

host

):

subprocess

.

check_call

(

[

'fab'

,

'reset_database'

,

'--host={}'

.

format

(

host

)],

cwd

=

THIS_FOLDER

)

Here we use the

subprocess

module to call some Fabric functions using the

fab

command.

As you can see, the command-line syntax for arguments to

fab

functions is quite

simple, a colon and then a variable=argument syntax.

Incidentally, this is also the first time I’ve shown the “new-style” string

formatting syntax. As you can see it uses curly brackets

{}

instead of

%s

. I slightly

prefer it to the old style, but you’re bound to come across both if you spend any

time with Python. Take a look at some of the examples in the

Python docs

to

learn more.

Because of all the hopping around via Fabric and subprocesses, we’re forced to

be quite careful about extracting the session key as a string from the output of

the command as it gets run on the server.

314

|

Chapter 17: Test Fixtures, Logging, and Server-Side Debugging