wrote "Writing test files" section in Readme.md

master
Dylan Lloyd 2013-05-04 16:54:58 -04:00
parent 201266bb0e
commit 92c59e4d3b
1 changed files with 41 additions and 0 deletions

View File

@ -6,6 +6,47 @@ Before reading this section, make sure you've read the [DuckDuckHack Intro Site]
### Example
![morse code example](https://s3.amazonaws.com/ddg-assets/docs/goodie_example.png)
## Writing test files
Every goodie includes a test file in the `t` directory. For example, the **RouterPasswords** goodie uses the the test file `t/RouterPasswords.t`. This test file includes sample queries and answers, and is run automatically before every release to ensure that all goodies are triggering properly with correct answers. The test file is a Perl program that uses the Perl packages `DDG::Test::Goodie` and `Test::More` to function. Here's an annotated excerpt from `t/RouterPasswords.t` that you can use as a base:
```
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use DDG::Test::Goodie;
# These zci attributes aren't necessary, but if you specify them inside your goodie,
# you'll need to add matching values here to check against.
zci answer_type => 'password';
zci is_cached => 1;
ddg_goodie_test(
[
# This is the name of the goodie that will be loaded to test.
'DDG::Goodie::RouterPasswords'
],
# This is a sample query, just like the user will enter into the DuckDuckGo search box
'Belkin f5d6130' =>
test_zci(
# The first argument to test_zci is the plain text (default) returned from a goodie.
# If your goodie also returns an HTML version, you can pass that along explicitly as
# the second argument. If your goodie is random, you can use regexs instead of
# strings to match against.
'Default login for the BELKIN F5D6130: Username: (none) Password: password',
html => 'Default login for the BELKIN F5D6130:<br><i>Username</i>: (none)<br><i>Password</i>: password'
),
# You should include more test cases here. Try to think of ways that your plugin
# might break, and add them here to ensure they won't.
);
done_testing;
```
Once you've written a test file, you can test it on it's own with `perl -Ilib t/GoodieName.t`.
## Advanced Goodies
These advanced handle techniques are specific to Goodie plugins: