Often have I seen or written a line like this:
use Test::More 'tests' => 23;
This tells Test::More that there will be 23 tests. If I add or remove a test, I also have to update the number of tests. I usually don't have to count them myself, though. I can just run the test script and see how many tests it runs. Nevertheless, this small extra hassle is one I'd like to avoid.
Sometimes I have data driven tests, and it looks like this:
use Test::More; # test definitions plan 'tests' => scalar @cases + 2 * scalar @cases_that_cause_two_tests + 17;
In this example, I have 17 tests that run regardless of the data, and I have some other tests based on data structures. Now when I make a change, it's not as easy to find out what that number should be. Running the script gives me some number > 17, but how many of those were driven by data and how many were the static tests?
So I've devised another way to count tests.
use Test::More; # This variable is set by BEGIN blocks elsewhere. my $static_tests; plan 'tests' => $static_tests; BEGIN { $static_tests++ } pass 'Static test 1'; BEGIN { $static_tests += 3 } pass 'Group test 1/3'; pass 'Group test 2/3'; pass 'Group test 3/3';
I think it's important to document the $static_tests variable because it's not immediately obvious where it gets its value.
In a real test script, I might want to stagger the counters. I could have a single BEGIN { $static_tests += 10 } for every ten cases. A longer test with a lot of setup would have a BEGIN { $static_tests++ } with it. I don't have to have more book keeping than actual testing.
I wonder if remembering the BEGIN block to go with the tests will become a problem. As long as the script isn't too long, it shouldn't be hard to go through it and make sure they all match up as they should. If the script is too long for that, I probably should break it up anyway.
I'd be interested to hear the thoughts of the other monks about this.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Counting test cases
by arc_of_descent (Hermit) on Mar 29, 2008 at 06:04 UTC | |
|
Re: Counting test cases
by Herkum (Parson) on Mar 29, 2008 at 14:22 UTC | |
|
Re: Counting test cases
by jplindstrom (Monsignor) on Mar 29, 2008 at 17:59 UTC | |
|
Re: Counting test cases
by talexb (Chancellor) on Apr 01, 2008 at 15:47 UTC | |
by kyle (Abbot) on Apr 01, 2008 at 16:02 UTC | |
|
Re: Counting test cases
by szabgab (Priest) on Mar 30, 2008 at 17:41 UTC | |
by kyle (Abbot) on Mar 30, 2008 at 18:28 UTC |