Python is getting to be quite popular as the language of choice for testers. One nice thing about Python is the philosophy that there should be “only one way to do it” — the exact opposite of Perl’s mantra “there’s more than one way to do it.”
This lends precision to Python scripts which might not be the case with other tools. Of course, above the bare language level, there are idioms to learn and a variety of tools that can be used.
Here are a number of tools written in or using python for testing:
unittest – this is a typical xUnit framework, and will probably be the base of most tests. There are the expected methods setUp() and tearDown(), as well as asserts. Tests extend TestCase, are grouped into TestSuites, and executed with TestRunners the default being TextTestRunner. Nothing surprising here for xUnit users, and a clear, concise way to get started for those new to writing code tests. A couple niceties for running unit tests in Python that show maturity (over JUnit, for example) are the TestLoader and TestResult classes, for organizing tests into suites and gathering results, respectively.
Selenium – Selenium RC is written in Java, but has a python driver. A good way to ease into Python test development is through the Selenium IDE which can convert recorded tests into Python. Not only will it help you learn the Selenium API and Python syntax, but it will also introduce you to unittest.
twill – twill is “a simple scripting language for Web browsing”. Think of it as a selenese-style interface for the grandmother of all web testing techniques, the venerable (and painful) telnet port 80. Another analogy would be using htmlunit from a command line. It looks very interesting for non-browser-based testing.
Pamie – Pamie is a port of Samie, a Perl framework for automating IE via COM. Watir (Watij, Watin) is a similar framework written in Ruby which is probably more mature. I don’t know if Pamie is still active.
Mechanize – There is also a port of Perl’s WWW::Mechanize to Python for those who like it.
WebUnit – WebUnit is an extension of unittest that provides additional features for web testing. A WebTestCase drives an HTTP Request and has additional helpers. It looks fairly simple and straightforward, and would fall into the same category of non-browser driven tests with Mechanize, Twill, HTMLUnit, and Webrat, though probably not as featureful. (But sometimes you don’t need all the features.)
Fitnesse – Fitnesse, like Selenium is written in Java, but fixtures can also be written in Python via Jython. There is also a port of Fit called PyFit, but it may be outdated. Personally, I like the idea of wiki documentation tied to user-editable test data and developer written test fixtures as code. I’d like to see updated Fit style testing frameworks with a better wiki than Fitnesse.
webtest – Not to be confused with Canoo Web Test, webtest “is an extraction of paste.fixture.TestApp" In a nutshell, it’s for testing at the WSGI application layer.
Saving the best for last…
doctest – Write literate tests using docstrings. Ability to execute tests from the Python shell. It’s like fitnesse in your source code.
nose – “nose extends unittest to make testing easier.” It’s the way the cool kids are writing their tests in Python these days. I don’t know much about it.
py.test – py.test is an alternate testing framework, that is also compatible with unittest (and nose). It’s quite different, emphasising simplicity (in code) with “no boilerplate test functions”. I haven’t quite wrapped my head around it, but the website says it is a “mature command line testing tool” that “automatically collects and executes tests” and “supports many testing practises and methods”. Perhaps it’s Python’s answer to Rspec, but going in a completely different direction. One that looks much more interesting to me.
See also the following: