Slashdot Mirror


Testing Frameworks in Python

An anonymous reader writes "This article looks at Python's two standard modules for unit testing: unittest and doctest. These modules expand on the capability of the built-in assert statement, which is used for validation of pre-conditions and post-conditions within functions. The author discusses the best ways to incorporate testing into Python development, weighing the advantages of different styles for different types of projects."

12 of 120 comments (clear)

  1. Traditional testers might be interested... by tcopeland · · Score: 5, Informative

    ...in Bret Pettichord's Scripting for Testers one day class.

    It talks about eliminating some of the tediousness from testing web applications, mainly by using automated solutions like WTR.

    He's also got a list of testing resources that's got some good stuff in there...

  2. bull by kpharmer · · Score: 5, Informative

    A few thoughts:

    1. I've never had a problem with memory management in python. Can't say it doesn't exist, just never impacted my production applications.

    2. Implementing great built-in test frameworks doesn't need to wait for memory management improvements. I'm seeing almost immediate pay-offs from this kind of built-in testing.

    3. I'm implementing python as an alternative to java in large applications - with complete success. Easy to learn, easy to maintain, fast enough to handle millions of rows of data a day - what's not to like?

  3. Testing.. by MosesJones · · Score: 4, Informative


    I hate to break it to the hack and slashdot crowd, but Testing is actually a whole career in itself, and the application of different testing processes and methods to different projects is a critical part of ensuring projects succeed.

    This article covers NOTHING about the different types of testing on a project, or indeed how test cases should even be constructed. Its basically about some UnitTesting elements that could be done by testing.

    I know its unpopular here on Slashdot to claim that there are more developers working on big projects than people hacking in Python. Buts its articles like these that underline the difference between professional software development and hacking.

    This is about hacking.

    --
    An Eye for an Eye will make the whole world blind - Gandhi
  4. Languages form an ecosystem by Anonymous Coward · · Score: 4, Insightful

    Judging from some previous comments, I see that some fail to grasp that modern computer languages form a large ecosystem. Each language has its purpose, and one can not easily dismiss a language as dead, just because some other, ostensibly more powerful language has appeared on the block. Monkeys, whales, cockroaches, ants, and plants continue to coexist with humans.

    When I want to solve a program I choose the language I will use, taking into account the abstractions and facilities it offers. * I chose Java when I wanted to leverage the javadoc applets (doclets) to convert a Java-like syntax into UML with my UMLgraph tool .
    * I chose C++ to implement the CScout refactoring browser for C programs. In this case I wanted rich and efficient data structures, with minimal speed and space overhead. CScout datasets can require more than 1GB of RAM, and runtimes can span more than a day; any overhead of object boxing, garbage collection, or bytecode interpretation would in this case be unacceptable.
    * I chose Perl to o Convert digital photographs and GPS track logs into annotated photo albums and trip maps
    o Examine the availability of 4500 URLs cited in computer science research papers.
    o To create the diagrams and the index for my book Code Reading: The Open Source Perspective.
    In all the above cases, I needed a typeless language with a rich set of operators, functions, and libraries to minimize the time I would spend to convert my ideas into code. Ruby and Python would have served me equally well.
    * Finally, I chose C to write
    o the *BSD sed implementation.
    o The socketpipe zero-overhead network pipe tool.
    o The Outwit Windows-Unix shell integration tool suite.
    o The fileprune backup file prune utility.
    o A device driver for interfacing with my home's alarm system.
    In these cases, I did not require any fancy data structures or framework APIs, but I did want tight integration with the underlying system, absolute efficiency, and minimum-fuss portability. For code that will be executed billions of times on tens of thousands of systems, spending some additional effort to provide the absolute efficiency and reasonable portability that are possible in C, is a proposition one should take into account.

  5. Re:Different types of project ? by kpharmer · · Score: 4, Interesting

    > Please go an get a decent, non-language specific book on testing before reading and listening to this
    > article.

    Sure usability, user acceptance, system, string, and integration testing are all valuable. But why can't developers without any knowledge of these start with built-in unit testing?

    And keep in mind that few books on testing cover the fairly recent phenomenon of test-driven-development (or test-centered-development).

    The use of built-in test harnesses and focus upon developing tests as documentation of requirements is probably the biggest single improvement in testing in twenty years.

    In one fell swoop the debugger is rendered obsolete...

  6. Re:true programmers... by JohnGrahamCumming · · Score: 5, Insightful

    True programmers never test their code, that is for people who are unsure of themselves and who should not be trusted

    Despite that fact that you are clearly trolling I think there's a valid point in what you are saying: there are programmers who think that they are too good to write unit or system tests for their code. And that's a real danger.

    The adage "a line of untested code is a line of broken code" is so often true that I still find it scary when examining untested code. It's just amazing how much of a difference the discipline of writing automatic unit tests makes in improving code.

    If you think you are too good to write tests, then perhaps you are too good for the software industry?

    John.

  7. Re:Python's dirty little secret by jemfinch · · Score: 5, Insightful

    You're not a "long time Python developer." Far from it. You may qualify as a "python developer from a long time ago," though -- in fact, that seems likely, given that the page you linked was last modified in 1999.

    It's Guido van Rossum, not Guido van Sustren. And Python's garbage collector works fine on cycles, as it has done since 2.0, iirc. The only way you'll get a memory leak with Python these days is by writing a C module that forgets some DECREFs or by writing cyclic objects with __del__ methods, and even in the latter case, you can easily take care of your leaks by breaking the cycle yourself.

    You're uninformed and incorrect; take your troll to the next Perl story, please :)

    Jeremy

  8. Re:Python's dirty little secret by smallpaul · · Score: 5, Informative

    It is clear you do not know anything about Python. You are not a long-time Python developer but rather a troll. The first hint is that you misspell Guido Van Rossum's name.

    The second hint is that you cite a page from 1999. A Python developer would know that that was before Python had cyclic garbage collection. Here is an article from 2001 that describes how the issue was fixed.

    The third hint is that you point to a page that describes how to avoid creating a memory leak by appending an infinite number of items to a list. Guess what: appending an infinite number of items to a list causes a memory leak in Java, C++, C, assembly, Scheme, sh, Perl and every other programming language in the world. If you ask the computer to store a continually growing list of items, it will do so...in any language.

    If you think that Python can leak memory in circumstances where other languages would not, post an example program and we'll all test it out.

  9. Jython for unit testing by Avumede · · Score: 4, Interesting

    I use Jython regularly for unit testing Java. The expressivness of jython is great for writing shorter unit tests, and the lack of compilation makes the whole write-test-debug cycle short. And, dare I say, it's just more fun to code in jython than Java.

  10. Unit tests are just one aspect of testing by DeadVulcan · · Score: 4, Insightful

    What you're saying puzzles me. You're absolutely right that this article is not about testing as a whole. The title should have been "Unit Testing Frameworks in Python."

    But your statement that "this is about hacking" and not professional software development puzzles me.

    I believe unit tests are a very legitimate piece of testing - a kind of first-line defence. They're intended to test individual software modules for their low-level behaviour. Typically, a developer would be expected to run them before submitting any change or bugfix, as a kind of "smoke test" to make sure things are okay. Certainly, some organizations might make the mistake of thinking that this kind of testing is all that's required - which is dreadfully wrong - but I don't think there's anything hackish about it.

    In a large organization, the testing team might not consider it testing because unit tests are necessarily maintained and performed by the developers only.

    But I would argue the exact opposite with regards to underlining the difference between professional software development and hacking. If you don't have unit tests, I would say that what you're doing is closer to hacking.

    --
    Accountability on the heads of the powerful.
    Power in the hands of the accountable.
  11. Re:Python's dirty little secret by Lulu+of+the+Lotus-Ea · · Score: 4, Informative

    FWIW, I'm the author of the referenced testing article. Though that's not really germane to the following comment.

    b.foster's misguided comment gives a link to a 1999 article about problems in Python with cleaning up circular references. Well, yeah: in 1999, Python 1.5.2 could leak on circular references. It was actually less of a problem in practice than advocates of mark-and-sweep collectors tended to complain about; but it really was a limitation. That was a long time ago.

    Nowadays, Python still uses references counting as its primary collection mechanism. It's a nice system: fast, deterministic, etc. (despite what you might think, refcount *really is* faster than Bohm generational; and is well-tested as such by very smart Python developers). However, the last half-decade of Python versions also perform occassional checks for orphaned objects, and cleans them up too. The "problem" pointed to is of historical interest, at most.

  12. Re:Different types of project ? by Lulu+of+the+Lotus-Ea · · Score: 4, Insightful

    How is the parent "insightful"?!

    I wrote a 2300 word article for a column on Python! I didn't write a book. Well, actually, I did write a book, but it isn't the above article (and it's not about testing frameworks). It's certainly not a good idea to think my short article is the alpha and omega of testing. But I am confident that my article -does- address a topic that some Python programmers can benefit from. And other installments of _Charming Python_ each address similarly small, but useful, topics.