Debian Codegraph @

docs/practical_tips.rst

raw
1 .. _id.practicaltips:
2
3 =========================
4 Practical Tips on Testing
5 =========================
6
7 This chapter contains a collection of tips on test strategies and tools, such
8 as test automation libraries, that help you make BDD a successful experience.
9
10 Seriously, Don't Test the User Interface
11 ========================================
12
13 .. warning::
14
15 While you can use :pypi:`behave` to drive the "user interface" (UI) or
16 front-end, interacting with the model layer or the business logic, e.g.
17 by using a REST API, is often the better choice.
18
19 And keep in mind, BDD advises your to test **WHAT** your application
20 should do and not **HOW** it is done.
21
22 If you want to test/exercise also the "user interface", it may be a good idea
23 to reuse the feature files, that test the model layer, by just replacing the
24 test automation layer (meaning mostly the step implementations).
25 This approach ensures that your feature files are technology-agnostic,
26 meaning they are independent how you interact with "system under test" (SUT) or
27 "application under test" (AUT).
28
29 For example, if you want to use the feature files in the same directory for
30 testing the model layer and the UI layer, this can be done by using the
31 ``--stage`` option, like with:
32
33 .. code-block:: bash
34
35 $ behave --stage=model features/
36 $ behave --stage=ui features/ # NOTE: Normally used on a subset of features.
37
38 See the :ref:`id.appendix.more_info` chapter for additional hints.
39
40 Automation Libraries
41 ====================
42
43 With *behave* you can test anything on your application stack: front-end
44 behavior, RESTful APIs, you can even drive your unit tests using Gherkin
45 language. Any library that helps you with that you usually integrate by
46 adding start-up code in ``before_all()`` and tear-down code in ``after_all()``.
47
48 The following examples show you how to interact with your Python application
49 by using the web interface (see `Seriously, Don't Test the User Interface`_
50 above to learn about entry points for test automation that may be better
51 suited for your use case).
52
53
54 Selenium (Example)
55 ------------------
56
57 To start a web browser for interaction with the front-end using
58 :pypi:`selenium` your ``environment.py`` may look like this:
59
60 .. code-block:: python
61
62 # -- FILE: features/environment.py
63 # CONTAINS: Browser fixture setup and teardown
64 from behave import fixture, use_fixture
65 from selenium.webdriver import Firefox
66
67 @fixture
68 def browser_firefox(context):
69 # -- BEHAVE-FIXTURE: Similar to @contextlib.contextmanager
70 context.browser = Firefox()
71 yield context.browser
72 # -- CLEANUP-FIXTURE PART:
73 context.browser.quit()
74
75 def before_all(context):
76 use_fixture(browser_firefox, context)
77 # -- NOTE: CLEANUP-FIXTURE is called after after_all() hook.
78
79 In your step implementations you can use the ``context.browser`` object to
80 access Selenium features. See the `Selenium docs`_ (``remote.webdriver``) for
81 details. Example using :pypi:`behave-django`:
82
83 .. code-block:: python
84
85 # -- FILE: features/steps/browser_steps.py
86 from behave import given, when, then
87
88 @when(u'I visit "{url}"')
89 def step_impl(context, url):
90 context.browser.get(context.get_url(url))
91
92 .. _Selenium docs: https://seleniumhq.github.io/selenium/docs/api/py/api.html
93
94
95 Splinter (Example)
96 ------------------
97
98 To start a web browser for interaction with the front-end using
99 :pypi:`splinter` your ``environment.py`` may look like this:
100
101 .. code-block:: python
102
103 # -- FILE: features/environment.py
104 # CONTAINS: Browser fixture setup and teardown
105 from behave import fixture, use_fixture
106 from splinter.browser import Browser
107
108 @fixture
109 def splinter_browser(context):
110 context.browser = Browser()
111 yield context.browser
112 context.browser.quit()
113
114 def before_all(context):
115 use_fixture(splinter_browser, context)
116
117
118 In your step implementations you can use the ``context.browser`` object to
119 access Splinter features. See the `Splinter docs`_ for details. Example
120 using *behave-django*:
121
122 .. code-block:: python
123
124 # -- FILE: features/steps/browser_steps.py
125 from behave import given, when, then
126
127 @when(u'I visit "{url}"')
128 def step_impl(context, url):
129 context.browser.visit(context.get_url(url))
130
131 .. _Splinter docs: http://splinter.readthedocs.io/en/latest/
132
133
134 Visual Testing
135 --------------
136
137 Visually checking your front-end on regression is integrated into *behave* in
138 a straight-forward manner, too. Basically, what you do is drive your
139 application using the front-end automation library of your choice (such as
140 Selenium, Splinter, etc.) to the test location, take a screenshot and compare
141 it with an earlier, approved screenshot (your "baseline").
142
143 A list of visual testing tools and services is available from Dave Haeffner's
144 `How to Do Visual Testing`_ blog post.
145
146 .. _How to Do Visual Testing:
147 http://testautomation.applitools.com/post/105435804567/how-to-do-visual-testing-with-selenium