Once again, I'm playing with another language. This time, I decided to put a fair amount of effort into learning Python. After returning the book I bought two days ago after discovering it was out of date, I simply grabbed the latest docs from the Python site and decided to sit down and start coding. Naturally, the first thing I wanted to do was learn how to write tests. I watched how our Python programmer at work wrote tests and I was surprised at how much code he had to write for it, so I started searching for Python testing frameworks myself. That's when I discovered Python's unittest. Here's how to write two tests:
import unittestIt appears that for each test case, I have to define a new class and then run tests on it. I'm totally blown away by the amount of work involved and all I want to do is write simple tests. While I realize that I might be reinventing the wheel, I decided that a great way to learn Python would be to write my own testing framework modelled after Perl's Test::More.
import mytestThat would produce output like:
1 .. 2
ok 1 - Default size should be correct
ok 2 - ... and resize should work
Results:
Success: 2
Failure: 0
Percent passed: 100.00
So far, that's much more like what I'm used to seeing and it was pretty easy to implement (though I have a lot of work to do). However, as I get further into Python, I can only assume that they have reasons for why they're doing things their way. I don't understand why they lack test counts, for example. Perhaps procedural tests (even if implemented through an object instance) simply don't fit the Python model very well, but we'll see what happens.
And for my first actual test program:
import testI get the following results:
1 .. 4Next I need to add many more testing methods and try my hand at producing a test harness. I may very well scrap all of this in the end but it's a great learning experience.
setUp
method) matchRe:Hmm...
Ovid on 2003-04-13T21:06:58
Ah, that has better testing information that the docs that I grabbed. Looks like it's fairly useful. Thanks!
Re:xUnit framework
pdcawley on 2003-04-14T08:32:32
Speaking as someone who used to be the maintainer of this package, I've switched to using Test::Unit, which place nicely with all the other testing frameworks that are built on top of Test::Builder. It does a few things different if you're used to the xUnit way of things, but I find it to be a really good synthesis of approaches.