Test::Cukes now assumes assertion-based testing code.

作者:   發佈於:  

Testing in Perl are usually done with Test::More, which is implements TAP -- The Test Anything Protocol . It's one good and simple way to do it, but I've always find it tiresome to keep updating the number of total tests.

After I realized how I can automate the test plans, I implemented in Test::Cukes.

It's really straight forward to understand it. Here's the code that maps scenario text into test blocks:

   Given qr/the test program is running/, sub {
      assert "running";

  When qr/it reaches this step/, sub {
      assert "reaches";

  Then qr/it should pass/, sub {
      assert "passes";


So each of these blocks simply represent one test. That is, one ok if writting with Test::More. Test::Cukes reports ok (pass) if the block exits normally, and reports not ok if the block dies.

The scenario text reads like this:

    Scenario: Hello World
      Given the test program is running
      When it reaches this step
      Then it should pass

For each lines in the the Given-When-Then block, it represent one test (one ok or not ok in the TAP output.) Therefore, the total number of tests is really just the total number of lines in the scenario text. Given such thought is cleared, when it comes to implementation, then it's just a matter of hacking :)

In the test blocks, you then use assert to examine your function output, values of variables, etc. Or you can do it on your own, just make sure it dies when the examination failed.

I think I started to like this idea and I shall keep polishing Test::Cukes so it can be a helpful tool to write module tests.