Bootstraping test infrastructure for mojo application using swat

Install swat mojo command

You need a swat mojo command to generate swat tests scaffolding for mojo application.

cpanm Mojolicious::Command::swat

Bootstrap mojo application

Then you need to bootstrap mojo application or choose existed one.

mkdir myapp
cd myapp
mojo generate lite_app myapp.pl

Define http resources ( mojo routes )

As well as define your routes.

$ nano myapp.pl

#!/usr/bin/env perl
use Mojolicious::Lite;

get '/' => sub {
  my $c = shift;
  $c->render(text => 'ROOT');
};


post '/hello' => sub {
  my $c = shift;
  $c->render(text => 'HELLO');
};

get '/hello/world' => sub {
  my $c = shift;
  $c->render(text => 'HELLO WORLD');
};

app->start;


$ ./myapp.pl routes
/             GET
/hello        POST  hello
/hello/world  GET   helloworld

Bootstrap swat tests

And then create a swat tests for every route exists.

$ ./myapp.pl swat
generate swat route for / ...
generate swat data for GET / ...
generate swat route for /hello ...
generate swat data for POST /hello ...
generate swat route for /hello/world ...
generate swat data for GET /hello/world ...

Specify additional swat check lists

This phase might be skipped as preliminary 200 OK checks are already added on bootstrap phase.

But you may define more.

For complete documentation on how to write swat tests please visit https://github.com/melezhik/swat.

$ echo ROOT >> swat/get.txt
$ echo HELLO >> swat/hello/post.txt
$ echo HELLO WORLD >> swat/hello/world/get.txt

Start mojo application

$ morbo ./myapp.pl
Server available at http://127.0.0.1:3000

Run swat tests

vagrant@Debian-jessie-amd64-netboot:~/projects/myapp$ swat swat/
/home/vagrant/.swat/.cache/26576/prove/hello/world/00.GET.t ..
ok 1 - GET http://127.0.0.1:3000/hello/world succeeded
# response saved to /home/vagrant/.swat/.cache/26576/prove/an7LtP99Ju
ok 2 - output match '200 OK'
ok 3 - output match 'HELLO WORLD'
1..3
ok
/home/vagrant/.swat/.cache/26576/prove/foo/bar/00.PUT.t ......
ok 1 - PUT http://127.0.0.1:3000/foo/bar succeeded
# response saved to /home/vagrant/.swat/.cache/26576/prove/L1JTyul6W5
ok 2 - output match '200 OK'
1..2
ok
/home/vagrant/.swat/.cache/26576/prove/00.GET.t ..............
ok 1 - GET http://127.0.0.1:3000/ succeeded
# response saved to /home/vagrant/.swat/.cache/26576/prove/YPnyXfYPXz
ok 2 - output match '200 OK'
ok 3 - output match 'ROOT'
1..3
ok
/home/vagrant/.swat/.cache/26576/prove/hello/00.POST.t .......
ok 1 - POST http://127.0.0.1:3000/hello succeeded
# response saved to /home/vagrant/.swat/.cache/26576/prove/baZKYm1hGD
ok 2 - output match '200 OK'
ok 3 - output match 'HELLO'
1..3
ok
All tests successful.
Files=4, Tests=12,  0 wallclock secs ( 0.03 usr  0.00 sys +  0.20 cusr  0.01 csys =  0.24 CPU)
Result: PASS

That's it.

Enjoy your testing with swat! ;--)

Leave a comment

About melezhik

user-pic Dev & Devops --- Then I beheld all the work of God, that a man cannot find out the work that is done under the sun: because though a man labour to seek it out, yet he shall not find it; yea further; though a wise man think to know it, yet shall he not be able to find it. (Ecclesiastes 8:17)