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."

35 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...

    1. Re:Traditional testers might be interested... by Anonymous Coward · · Score: 2, Insightful

      how about "testing for scripters" to avoid all those errors the languages allow to happen?

  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. Re:Python's dirty little secret by tuffy · · Score: 3, Interesting
    Despite all of Guido Van Sustren's claims to the contrary, Python's garbage collector just doesn't work correctly, allowing the programmer to create a circular reference which never gets resolved.

    Circular references are what weak references are meant to handle. But it'd be nice if the garbage collector would handle those cases automatically.

    --

    Ita erat quando hic adveni.

  4. Re:Python's dirty little secret by gaj · · Score: 3, Informative
    Um ... you'd have much more credibility if you at least got his name right.

    It's Guido van Rossum.

    As for your claims about the GC, I was under the impression that current versions of Python have a hybrid GC that cleans up circ-refs. I certainly could be mistaken, though, as I've been exiled to Java-land for the last several years.

  5. 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
    1. Re:Testing.. by Spoing · · Score: 3, Interesting
      1. 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.

      Agreed. I can impress someone with my knowledge and professionalism, but when they ask what languages I can program in and I state "I'm not a developer", they just don't get it. That I can whip a program up is one thing...that I know that I'm not the person to do that seems to lead to puzzlement.

      These days, testing (and CM) is so far off the radar for most folks that it is no longer considered an issue. Yet, developers are constantly required to know all details of the specs and to deliver code that does not immediately fall apart. Some can, but very few can do it rapidly.

      The extra burden of detailed testing -- work that the developers are usually not suited for or have time for anyway -- does not make for a productive environment. Limited testing is OK though full testing by development is a distraction -- both VV&T and development groups know it.

      That said, initial unit tests of the core services can be the responsibility of the development group, though it should never fall to the same person writing the code being tested. The unit test framework will be incomplete and faulty since the developer does not have the proper distance from the code they are testing.

      A skilled VV&T developer can do the job much better, though time for that person to write code to test the developer's work is usually not allocated in the schedule...even if such a person is hired at all.

      (Case in point: I was hired last year to automate testing and test 2 applications...10 months later, and here I am having tested about 20 applications and have not written any automated test sripts. Yes, I warned them that it takes a specific type of environment to support automated testing, one that can allow substantial amounts of time and effort to creating and maintaing those scripts, and I didn't see that environment. Yes, they understood and said 'do it anyway'. Yep, that worked.)

      --
      A firewall can not protect you from yourself. Turn off what you do not need. Do not use the firewall to do your work.
    2. Re:Testing.. by SurfTheWorld · · Score: 2, Interesting

      What is it about testers and their negative reactions to developers writing tests? Is it out of fear of job security? I don't understand why so many testers frown upon or discard tests written by developers as worthless, or second rate, to the tests that they have written. Indeed, many times what accompanies test dismissal is the comment of "Developers don't know how to test software." Nevermind the lack of supporting details to back up this broad generalization.

      What to me is so strikingly insane is that at the most primitive levels, testers write tests. What kinds of tests they write vary in size, scope and purpose. Developers, through unit tests, are technically doing part of the the tester's job. They produce tests! Sure, they are unit tests, but they are still valuable from the standpoint of software verification. Even if a tester walks away from them with simply a better understanding of how the software is put together, does that not make them valuable or important?

      Too many times do testers toss away unit tests as worthless (or in Moses' case "hackish") simply because they were not written by a tester. Tell me what qualifications you must have to be a tester and to write tests. What are your criteria for considering a test valid or valuable? Now tell me how I can run the litmus test against my test to see if it passes what you consider to be the qualifications for inclusion into a suite of tests.

      Were the best interests of the project (not the individual) ranked at the top of the importance list, I would expect testers to gleefully accept developer unit tests. Not only do they show accuracy in the software, but they more importantly demonstrate a developer's willingness to test and prove functionality. "Here's my code, it implements requirements X, Y, and Z, and here are the unit tests that demonstrate the functionality." Given that, a tester needs to tie together a high level interaction test or execute the regression tests. But the developer has gone a long way to help out the tester, and more times than not the tester pushes back with some assinine comment like "developers don't know how to write tests."

      Successful software teams are not like the 4x2 tetris block, where the design team hands off UML to the developers to implement, who then hand off the code in an assembled version to the testers to verify. Successful software teams are a coagulation of the + tetris blocks where developers perform some requirements analysis and testing while at the same time coding. Testers pick up some system administration. And system administrators do some testing and even development. This type of cross-pollinated structure on a team keeps code velocity high, job competition to a minimum, and leads to higher overall satisfaction (generally speaking, of course ;-)).

      Your view of unit tests as hacks without the proper overall test plan shows your inflexibility to cohabitate with developers willing to test on your project. Archaeic views like this usually lead to unsuccessful projects. Take a moment to look back over your resume and reflect on the various software projects you've worked on. Ask yourself what developement environment existed, and if the project was successful. My guess is you'll notice a pattern.

      --
      Do it for da shorties
  6. 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.

  7. Re:Python's dirty little secret by AKAImBatman · · Score: 2, Interesting

    Are you saying that this is an inherent language problem, or simply an implementation problem? i.e. Does the Jython interpreter display this issue, or only the native Python compiler/interpreter?

  8. 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...

  9. Re:Different types of project ? by tcopeland · · Score: 2, Insightful

    > probably the biggest single improvement
    > in testing in twenty years

    Right on. Being able to make some changes and run a suite of 500+ tests to make sure things still work is a thing of beauty.

    And when a bug slips through, that's just an opportunity to write another test and make sure that bug never happens again. Good stuff.

  10. Re:Python's dirty little secret by Anonymous Coward · · Score: 3, Informative

    "As for your claims about the GC, I was under the impression that current versions of Python have a hybrid GC that cleans up circ-refs."


    According to this you are correct.

    BUT.. please note it also says "You can run gc.collect() to force a collection, but there are pathological cases where objects will never be collected."

    It also mentions the use of weakrefs. :)
  11. 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.

  12. 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

  13. Re:Python's dirty little secret by gregfortune · · Score: 3, Insightful

    Despite all of Guido Van Sustren's claims to the contrary, Python's garbage collector just doesn't work correctly, allowing the programmer to create a circular reference which never gets resolved. This is a critical impediment to writing mission critical applications in Python, as they will eventually run out of memory and fail.

    Neither of those references point out he weakref module that allows you to work nicely with circular references.

    Python's memory issues need to be fixed before the language can break out of its niche.

    Yep, it's a niche language. Crawl back under your bridge.

  14. 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.

  15. Gentoo by bonch · · Score: 2, Interesting

    For using a niche language, Portage sure seems pretty popular.

  16. 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.

  17. Re:Different types of project ? by Stultsinator · · Score: 2, Interesting

    I agree that those topics are worthy of deeper discussion, but I think the article stands well on its own. I'm learning Python and I think the unittest section will help greatly while stumbling through the process of building applications.

    The links he provides in the Resources section at the end of the article provide well-rounded background information on testing in general.

  18. 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.
  19. Re:Different types of project ? by pavon · · Score: 2, Insightful

    Call me silly here, but that article talks about developer ONLY testing, and doesn't seem to discuss different types of projects at all. This was about basic code testing, and mainly unit-test.

    Yes, this is exacly what this article is about. What is wrong with that?

    No UAT, no System Test, no Integration Test... no how test cases should be defined.

    No it is assumed that anyone programmer worth his salt knows some test methodology, and this usefull article explains tools that make that task easier.

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

    Yes I agree, if you don't know these things you should learn them. But you act as thought this article is a waste of the electrons it traveled over, while it is actually quite usefull. I program python, learned about test methodology years ago but didn't learn about these tools until today.

    I don't see how you got +5 Insightfull for pointing out that this is article is not the all-inclusive source of testing knowledge. Everything ever written is limited in scope, that doesn't make it less usefull for the purpose it was written.

  20. Re:Python's dirty little secret by be-fan · · Score: 2, Insightful

    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.
    Properly implemented, Scheme should never leak memory. Even if you try to add an infinite number of items to a collection, you'll get an out-of-memory error before you'd get a leak.

    --
    A deep unwavering belief is a sure sign you're missing something...
  21. Re:Why Python? by leshert · · Score: 3, Insightful
    A good point. The idea of reducing the number of lines of code isn't just to reduce the amount of typing--it's mostly to reduce the amount of code you have to _read_ to understand what's going on.

    For example, it's easier to see what's going on in:

    for line in file("/var/log/messages"):
    processLine(line)

    than in the equivalent C or Java code. Also, terseness isn't the ultimate goal--I could make this more terse (and less readable or efficient) by saying:

    [processLine(line) for line in file("/var/log/messages")]

    The biggest benefit of Python over Java, as far as I'm concerned, is that code written in Python looks like what it does--that's the source of the "Python is executable pseudocode" meme you often hear from Python fanboys. The lack of a compile step and the dynamic typing help, but they're secondary to me.

  22. 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.

  23. Re:Python's dirty little secret by smallpaul · · Score: 3, Informative

    Argh...the garbage collector does handle circular references automatically and has done so for at least two years. Weak references are something totally different and are used e.g. for caches.

  24. 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.

  25. top of the foodchain by Doc+Ruby · · Score: 2, Insightful

    Unfortunately you had to use HTML to render an outline, when you'd have used PostScript if you had a choice. The reality of programming is the necessity of working with other people, including programmers thinking in the paradigm of the moment (think 1990s Perl), graphic designers/artists with Flash on the brain, marketers with buzzword colored glasses, legacy systems, and customers seeing their businesses modeled explicitly for the first time in any terms. I'd prefer compiling everyone's flowcharts to C++/IDL/VHDL, but that environment is at least 5 years away. Meanwhile, all we have it this year's hammer, and everything looks like a nail.

    --

    --
    make install -not war

  26. Re:Python's dirty little secret by Anonymous Coward · · Score: 3, Funny
    So the article he linked to is a "historical document", not something we should take seriously?

    Sorry bud, I think I've heard that line before.

  27. Re:Different types of project ? by Simon+Lyngshede · · Score: 2, Informative

    It is a very short article about using the unit testing framework for Python, not the definitive guide to testing. Do you even understand how amazingly large a subject software testing is?

    I think that unit testing is an important tool, it allow you to easily continue to do integration test and regression testing, if you know what you're doing.

    I will however agree with you on the "get a book on testing" part. I think it's important that anyone who develop software, understand what is involved in testing.

  28. Re:Why Python? by dubious9 · · Score: 3, Informative

    First off, I had mod points and I feel bad about not modding you up, and you don't deserve a 0:offtopic score.But I though a responce would be better.

    Can someone help me understand why I would want to use Python as opposed to Java?

    Python is not just about brevity, but I think you underestimate writing less lines of code. Take the fact that if you are writing twice as much code, you're probably writing twice as many bugs. Also a consise 50K LOC program is much easier to get around in than a 200K program.

    But anyway, Python is an interpreted, interactive, object-oriented programming language., where as Java is only the last of those three. Python also has dynamic documentation, meaning that the documentation string can be inspected at run-time, without the need to maintain a separate doc tree, ie Javadoc.

    Classes are dynamic ie. you can change their behavior at runtime. All python objects can be marshalled or serialized, while in Java you have to define the interface, and account for anything that you use that can't be serialized.

    There are also thing which I am indifferent about like tabbed blocks, and some things which I don't like, like not having enough compile time type checking. IMHO you should always define the type of a function parameter, it makes learning the API faster.

    In general, Python is great for script-strength stuff, ie. in situations where you would use perl. I can see it getting unwieldly as it got bigger but it generally scales much better then perl. Java tends to be too complex and too restrictive. In short there are many situations where you would choose one over the other.

    --
    Why, o why must the sky fall when I've learned to fly?
  29. Integrating the two approaches by teknico · · Score: 2, Informative

    Here is a presentation, with PDF article, about integrating the two approaches, straight from recent PyCon:

    Literate Testing: Automated Testing with doctest

  30. Re:Why Python? by Daath · · Score: 2, Informative

    Try reading ESR's Why Python?, also check out the Quotes on Python from python.org :)
    It's a beautiful language, I'm sorry I can't code in it more...

    --
    Any technology distinguishable from magic, is insufficiently advanced.
  31. Re:Python's dirty little secret by slinkp · · Score: 2, Interesting

    I've been running Zope (implemented in python) on production servers for years. The zope process typically has uptime of weeks and downtime is typically only of the scheduled variety. In the past 20 months I've been at my current job, I can count the number of Zope crashes on one hand. OTOH, we also run Java app servers (formerly BEA, now Jboss) and they crash all the time... frequently with out-of-memory errors.

    Snide remarks about "dirty little secrets" notwithstanding, I know which platform I trust to keep running.

  32. Re:true programmers... by Abcd1234 · · Score: 2, Insightful

    You know, the funny thing about this is that a good coder must want to see their code actually run, right? After all, everyone wants immediate gratification. And the beauty of unit tests is it provides just that... you can actually run your code in a controlled environment easily without the need to start up an entire application/framework/etc. This is especially true of large and/or distributed applications where there's a great deal of overhead in running a full system test.

    Moreover, unit tests make the test-develop cycle a *lot* shorter, especially on large projects, since you can easily run your code. Thus, you're wasting less time testing and more time coding... sounds like a great deal to me!

    So, in my mind, any coder that doesn't feel like writing tests isn't, in fact, a particularly good coder. Or, perhaps, they're doing their job for all the wrong reasons. I code because I want to see the fruits of my labour, and unit tests are a great way to quickly and easily see the results of my work.