Akihiro's Programmer Blog

Technology Notes for Personal

プロジェクトのユニットテスト用雛形をサクッと作る

この記事は、Perl5 Advent Calendar 2015 21日目の記事です。

昨日は @kfly8さんの 「About Dancer2 (ja)」 でした。

今回は、プロジェクトのユニットテストを作る上でちょこっと楽が出来るかもしれないスクリプトを作ったので晒します。(走り書きをお許し下さい


それがこいつです。


なにこれ

こいつは以下の様なことをしてくれます。

  1. lib以下にあるモジュール + メソッド を全て抽出
  2. t以下にテストファイルとして吐き出す


f:id:akihiro_0228:20151221204650p:plain


使い方

テストが存在しないこんなプロジェクトも。

$ tree lib
lib
├── Sample
│   ├── DB
│   │   ├── Row.pm
│   │   └── Schema.pm
│   ├── DB.pm
│   ├── Web
│   │   ├── Dispatcher.pm
│   │   ├── Plugin
│   │   │   └── Session.pm
│   │   ├── View.pm
│   │   └── ViewFunctions.pm
│   └── Web.pm
└── Sample.pm

4 directories, 9 files

$ tree t
t

0 directories, 0 files


ポンと叩けばあら不思議。

$ carton exec perl -Ilib gene.pl
CREATE: t/sample.t
CREATE: t/sample/db.t
CREATE: t/sample/web.t
CREATE: t/sample/db/row.t
CREATE: t/sample/db/schema.t
CREATE: t/sample/web/dispatcher.t
CREATE: t/sample/web/view.t
CREATE: t/sample/web/view_functions.t
CREATE: t/sample/web/plugin/session.t


こんなコケまくるテストが、

use strict;
use warnings;
use utf8;
use Test::More;

subtest 'context' => sub {
    ok 0, "Please write the test!";
};

subtest 'context_guard' => sub {
    ok 0, "Please write the test!";
};

subtest 'db' => sub {
    ok 0, "Please write the test!";
};

subtest 'set_context' => sub {
    ok 0, "Please write the test!";
};

done_testing;


量産される!

$ tree t
t
├── sample
│   ├── db
│   │   ├── row.t
│   │   └── schema.t
│   ├── db.t
│   ├── web
│   │   ├── dispatcher.t
│   │   ├── plugin
│   │   │   └── session.t
│   │   ├── view.t
│   │   └── view_functions.t
│   └── web.t
└── sample.t

4 directories, 9 files


終わりに

テストを書くことが何よりの生きがいだという人が使ってくれることを信じて。


次回は @magnolia_k_さん です!