1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
|
:LastChangedDate: $LastChangedDate$
:LastChangedRevision: $LastChangedRevision$
:LastChangedBy: $LastChangedBy$
Unit Tests in Twisted
=====================
Each *unit test* tests one bit of functionality in the
software. Unit tests are entirely automated and complete quickly.
Unit tests for the entire system are gathered into one test suite,
and may all be run in a single batch. The result of a unit test
is simple: either it passes, or it doesn't. All this means you
can test the entire system at any time without inconvenience, and
quickly see what passes and what fails.
Unit Tests in the Twisted Philosophy
------------------------------------
The Twisted development team adheres to the practice of `Extreme Programming <http://c2.com/cgi/wiki?ExtremeProgramming>`_ (XP),
and the usage of unit tests is a cornerstone XP practice. Unit tests are a
tool to give you increased confidence. You changed an algorithm -- did you
break something? Run the unit tests. If a test fails, you know where to
look, because each test covers only a small amount of code, and you know it
has something to do with the changes you just made. If all the tests pass,
you're good to go, and you don't need to second-guess yourself or worry that
you just accidentally broke someone else's program.
What to Test, What Not to Test
------------------------------
You don't have to write a test for every single
method you write, only production methods that could possibly break.
-- Kent Beck, Extreme Programming Explained
Running the Tests
-----------------
How
~~~
From the root of the Twisted source tree, run
`Trial <https://twistedmatrix.com/trac/wiki/TwistedTrial>`_ :
.. code-block:: console
$ bin/trial twisted
You'll find that having something like this in your emacs init
files is quite handy:
::
(defun runtests () (interactive)
(compile "python /somepath/Twisted/bin/trial /somepath/Twisted"))
(global-set-key [(alt t)] 'runtests)
When
~~~~
Always, always, *always* be sure `all the tests pass <https://ronjeffries.com/xprog/classics/expunittestsat100/>`_ before committing any code. If someone else
checks out code at the start of a development session and finds
failing tests, they will not be happy and may decide to *hunt you down* .
Since this is a geographically dispersed team, the person who can help
you get your code working probably isn't in the room with you. You may want
to share your work in progress over the network, but you want to leave the
main Git tree in good working order.
So `use a branch <https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging>`_ ,
and merge your changes back in only after your problem is solved and all the
unit tests pass again.
Adding a Test
-------------
Please don't add new modules to Twisted without adding tests
for them too. Otherwise we could change something which breaks
your module and not find out until later, making it hard to know
exactly what the change that broke it was, or until after a
release, and nobody wants broken code in a release.
Tests go into dedicated test packages such as
``twisted/test/`` or ``twisted/conch/test/`` ,
and are named ``test_foo.py`` , where ``foo`` is the name
of the module or package being tested. Extensive documentation on using
the PyUnit framework for writing unit tests can be found in the
:ref:`links section <core-development-policy-test-standard-links>` below.
One deviation from the standard PyUnit documentation: To ensure
that any variations in test results are due to variations in the
code or environment and not the test process itself, Twisted ships
with its own, compatible, testing framework. That just
means that when you import the unittest module, you will ``from twisted.trial import unittest`` instead of the
standard ``import unittest`` .
As long as you have followed the module naming and placement
conventions, ``trial`` will be smart
enough to pick up any new tests you write.
PyUnit provides a large number of assertion methods to be used when
writing tests. Many of these are redundant. For consistency, Twisted
unit tests should use the ``assert`` forms rather than the
``fail`` forms. Also, use ``assertEqual`` ,
``assertNotEqual`` , and ``assertAlmostEqual`` rather
than ``assertEquals`` , ``assertNotEquals`` , and
``assertAlmostEquals`` . ``assertTrue`` is also
preferred over ``assert_`` . You may notice this convention is
not followed everywhere in the Twisted codebase. If you are changing
some test code and notice the wrong method being used in nearby code,
feel free to adjust it.
When you add a unit test, make sure all methods have docstrings
specifying at a high level the intent of the test. That is, a description
that users of the method would understand.
Test Implementation Guidelines
------------------------------
Here are some guidelines to follow when writing tests for the Twisted
test suite. Many tests predate these guidelines and so do not follow them.
When in doubt, follow the guidelines given here, not the example of old unit
tests.
Naming Test Classes
~~~~~~~~~~~~~~~~~~~
When writing tests for the Twisted test suite, test classes are named
``FooTests``, where ``Foo`` is the name of the component being tested.
Here is an example:
.. code-block:: python
class SSHClientTests(unittest.TestCase):
def test_sshClient(self):
foo() # the actual test
Real I/O
~~~~~~~~
Most unit tests should avoid performing real, platform-implemented I/O operations.
Real I/O is slow, unreliable, and unwieldy.
When implementing a protocol, :py:class:`twisted.internet.testing.StringTransport` can be used instead of a real TCP transport.
``StringTransport`` is fast, deterministic, and can easily be used to exercise all possible network behaviors.
If you need pair a client to a server and have them talk to each other, use ``twisted.test.iosim.connect`` with ``twisted.test.iosim.FakeTransport`` transports.
Real Time
~~~~~~~~~
Most unit tests should also avoid waiting for real time to pass.
Unit tests which construct and advance a :py:class:`twisted.internet.task.Clock` are fast and deterministic.
When designing your code allow for the reactor to be injected during tests.
.. code-block:: python
from twisted.internet.task import Clock
def test_timeBasedFeature(self):
"""
In this test a Clock scheduler is used.
"""
clock = Clock()
yourThing = SomeThing()
yourThing._reactor = clock
state = yourThing.getState()
clock.advance(10)
# Get state after 10 seconds.
state = yourThing.getState()
Test Data
~~~~~~~~~
Keeping test data in the source tree should be avoided where possible.
In some cases it can not be avoided, but where it's obvious how to do so, do it.
Test data can be generated at run time or stored inside the test modules as constants.
When file system access is required, dumping data into a temporary path during the test run opens up more testing opportunities.
Inside the temporary path you can control various path properties or permissions.
You should design your code so that data can be read from arbitrary input streams.
Tests should be able to run even if they are run inside an installed copy of Twisted.
.. code-block:: python
publicRSA_openssh = ("ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAGEArzJx8OYOnJmzf4tf"
"vLi8DVPrJ3/c9k2I/Az64fxjHf9imyRJbixtQhlH9lfNjUIx+4LmrJH5QNRsFporcHDKOTwTT"
"h5KmRpslkYHRivcJSkbh/C+BR3utDS555mV comment")
def test_loadOpenSSHRSAPublic(self):
"""
L{keys.Key.fromStrea} can load RSA keys serialized in OpenSSH format.
"""
keys.Key.fromStream(StringIO(publicRSA_openssh))
The Global Reactor
~~~~~~~~~~~~~~~~~~
Since unit tests are avoiding real I/O and real time, they can usually avoid using a real reactor.
The only exceptions to this are unit tests for a real reactor implementation.
Unit tests for protocol implementations or other application code should not use a reactor.
Unit tests for real reactor implementations should not use the global reactor, but should
instead use ``twisted.internet.test.reactormixins.ReactorBuilder`` so they can be applied to all of the reactor implementations automatically.
In no case should new unit tests use the global reactor.
Skipping Tests
--------------
Trial, the Twisted unit test framework, has some extensions which are
designed to encourage developers to add new tests. One common situation is
that a test exercises some optional functionality: maybe it depends upon
certain external libraries being available, maybe it only works on certain
operating systems. The important common factor is that nobody considers
these limitations to be a bug.
To make it easy to test as much as possible, some tests may be skipped in
certain situations. Individual test cases can raise the ``SkipTest`` exception to indicate that they should be skipped, and
the remainder of the test is not run. In the summary (the very last thing
printed, at the bottom of the test output) the test is counted as a"skip" instead of a "success" or "fail" . This should be used
inside a conditional which looks for the necessary prerequisites:
.. code-block:: python
class SSHClientTests(unittest.TestCase):
def test_sshClient(self):
if not ssh_path:
raise unittest.SkipTest("cannot find ssh, nothing to test")
foo() # do actual test after the SkipTest
You can also set the ``.skip`` attribute on the method, with a
string to indicate why the test is being skipped. This is convenient for
temporarily turning off a test case, but it can also be set conditionally (by
manipulating the class attributes after they've been defined):
.. code-block:: python
class SomeThingTests(unittest.TestCase):
def test_thing(self):
dotest()
test_thing.skip = "disabled locally"
.. code-block:: python
class MyTests(unittest.TestCase):
def test_one(self):
...
def test_thing(self):
dotest()
if not haveThing:
MyTests.test_thing.im_func.skip = "cannot test without Thing"
# but test_one() will still run
Finally, you can turn off an entire TestCase at once by setting the .skip
attribute on the class. If you organize your tests by the functionality they
depend upon, this is a convenient way to disable just the tests which cannot
be run.
.. code-block:: python
class TCPTests(unittest.TestCase):
...
class SSLTests(unittest.TestCase):
if not haveSSL:
skip = "cannot test without SSL support"
# but TCPTests will still run
...
Testing New Functionality
-------------------------
Two good practices which arise from the "XP" development process are
sometimes at odds with each other:
- Unit tests are a good thing. Good developers recoil in horror when
they see a failing unit test. They should drop everything until the test
has been fixed.
- Good developers write the unit tests first. Once tests are done, they
write implementation code until the unit tests pass. Then they stop.
These two goals will sometimes conflict. The unit tests that are written
first, before any implementation has been done, are certain to fail. We want
developers to commit their code frequently, for reliability and to improve
coordination between multiple people working on the same problem together.
While the code is being written, other developers (those not involved in the
new feature) should not have to pay attention to failures in the new code.
We should not dilute our well-indoctrinated Failing Test Horror Syndrome by
crying wolf when an incomplete module has not yet started passing its unit
tests. To do so would either teach the module author to put off writing or
committing their unit tests until *after* all the functionality is
working, or it would teach the other developers to ignore failing test
cases. Both are bad things.
".todo" is intended to solve this problem. When a developer first
starts writing the unit tests for functionality that has not yet been
implemented, they can set the ``.todo`` attribute on the test
methods that are expected to fail. These methods will still be run, but
their failure will not be counted the same as normal failures: they will go
into an "expected failures" category. Developers should learn to treat
this category as a second-priority queue, behind actual test failures.
As the developer implements the feature, the tests will eventually start
passing. This is surprising: after all those tests are marked as being
expected to fail. The .todo tests which nevertheless pass are put into a"unexpected success" category. The developer should remove the .todo
tag from these tests. At that point, they become normal tests, and their
failure is once again cause for immediate action by the entire development
team.
The life cycle of a test is thus:
#. Test is created, marked ``.todo`` . Test fails: "expected failure" .
#. Code is written, test starts to pass. "unexpected success" .
#. ``.todo`` tag is removed. Test passes. "success" .
#. Code is broken, test starts to fail. "failure" . Developers spring
into action.
#. Code is fixed, test passes once more. "success" .
``.todo`` may be of use while you are developing a feature, but by the time you are ready to commit anything all the tests you have written should be passing.
In other words **never** commit to trunk tests marked as ``.todo``.
For unfinished tests you should create a follow up ticket and add the tests to the ticket's description.
You can also ignore the ``.todo`` marker and just make sure you write test first to see them failing before starting to work on the fix.
Line Coverage Information
-------------------------
Trial provides line coverage information, which is very useful to ensure
old code has decent coverage. Passing the ``--coverage`` option to Trial will generate the coverage information in a file called ``coverage`` which can be found in the ``_trial_temp``
folder.
Associating Test Cases With Source Files
----------------------------------------
Please add a ``test-case-name`` tag to the source file that is
covered by your new test. This is a comment at the beginning of the file
which looks like one of the following:
.. code-block:: python
# -*- test-case-name: twisted.test.test_defer -*-
or
.. code-block:: python
#!/usr/bin/env python
# -*- test-case-name: twisted.test.test_defer -*-
This format is understood by emacs to mark "File Variables" . The
intention is to accept ``test-case-name`` anywhere emacs would on
the first or second line of the file (but not in the ``File Variables:`` block that emacs accepts at the end of the file). If you
need to define other emacs file variables, you can either put them in the ``File Variables:`` block or use a semicolon-separated list of
variable definitions:
.. code-block:: python
# -*- test-case-name: twisted.test.test_defer; fill-column: 75; -*-
If the code is exercised by multiple test cases, those may be marked by
using a comma-separated list of tests, as follows: (NOTE: not all tools can
handle this yet.. ``trial --testmodule`` does, though)
.. code-block:: python
# -*- test-case-name: twisted.test.test_defer,twisted.test.test_tcp -*-
The ``test-case-name`` tag will allow ``trial --testmodule twisted/dir/myfile.py`` to determine which test cases need
to be run to exercise the code in ``myfile.py`` . Several tools (as
well as https://launchpad.net/twisted-emacs's ``twisted-dev.el`` 's F9 command) use this to automatically
run the right tests.
Links
-----
.. _core-development-policy-test-standard-links:
- A chapter on `Unit Testing <https://www.diveintopython3.net/unit-testing.html>`_
in Mark Pilgrim's `Dive Into Python <https://www.diveintopython3.net/>`_ .
- :mod:`unittest` module documentation, in the `Python Library Reference <https://docs.python.org/3/library>`_ .
- `UnitTest <http://c2.com/cgi/wiki?UnitTest>`__ on
the `PortlandPatternRepository Wiki <http://c2.com/cgi/wiki>`_ , where all the cool `ExtremeProgramming <http://c2.com/cgi/wiki?ExtremeProgramming>`_ kids hang out.
- `Unit Tests <http://www.extremeprogramming.org/rules/unittests.html>`_ in `Extreme Programming: A Gentle Introduction <http://www.extremeprogramming.org>`_ .
- Ron Jeffries expounds on the importance of `Unit Tests at 100% <https://ronjeffries.com/xprog/classics/expunittestsat100/>`_ .
- Ron Jeffries writes about the `Unit Test <https://web.archive.org/web/20140708115244/http://www.xprogramming.com/Practices/PracUnitTest.html>`_ in the `Extreme Programming practices of C3 <https://web.archive.org/web/20140827044941/http://www.xprogramming.com/Practices/xpractices.htm>`_ .
- `PyUnit's homepage <http://pyunit.sourceforge.net>`_ .
- The top-level tests directory, `twisted/test <https://github.com/twisted/twisted/tree/trunk/twisted/test>`_.
See also :doc:`Tips for writing tests for Twisted code </core/howto/testing>` .
|