Your SlideShare is downloading. ×
Jenkinsableじゃないプロジェクトをjenkinsableにするまでperl編
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Jenkinsableじゃないプロジェクトをjenkinsableにするまでperl編

826

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
826
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
8
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Jenkinsableじゃないプロジェクトを JenkinsableにするまでPerl編 太田健一郎 @oota_ken
  • 2. LLではコンパイルがな いJenkinsの最も分かりやすい導入効果であるコンパイルの自動実行がLLでは使えない自動テストがないプロジェクトで自動テストを導入するのは大変じゃ、どうすればいい?
  • 3. 静的検証 on Unit Test Test::Perl::Criticuse strict;use warnings;use Test::More;use Test::Perl::Critic;use Todo::Schema::Result::User;critic_ok($INC{"Todo/Schema/Result/User.pm"});done_testing;1;
  • 4. 静的検証 on Unit TestTest::Perl::Metrics::Lite use strict; use warnings; use Test::More; use Test::Perl::Critic; use Test::Perl::Metrics::Lite(-loc => 100, -mccabe_complexity => 20); use Todo::Schema::Result::User; my $user_path = $INC{"Todo/Schema/Result/User.pm"}; critic_ok($user_path); all_metrics_ok($user_path); 1;
  • 5. 静的検証 on Unit Test組み込み簡単テストが失敗すると直してもらえる普通に静的検証 on Jenkinsより効果高いベストプラクティスが身につく
  • 6. Test Case GeneratorTodo$ perl -MTest::StubGenerator -e my $stub = Test::StubGenerator->new({ file =>"lib/Todo/Schema/Result/User.pm" }); print $stub->gen_testfile;#!/usr/bin/perluse strict;use warnings;use Test::More qw/no_plan/;use lib ..;BEGIN { use_ok(Todo::Schema::Result::User); }# And now to test the methods/subroutines.ok( $obj->age(), can call $obj->age() without params );ok( $obj->get_all_todoes_character_length(), can call $obj->get_all_todoes_character_length() without params );
  • 7. モックオブジェクト Test::MockObjectuse Test::MockObject;use Test::MockObject::Extends;is($oota->age($day_20130206), 37, "ootakens age is 37 on 2013/02/06");my $todo_mock_first = Test::MockObject->new;$todo_mock_first->set_always("memo", "12345");my $todo_mock_second = Test::MockObject->new;$todo_mock_second->set_always("memo", "123");my $user_mock = Test::MockObject::Extends->new(Todo::Schema::Result::User->new);$user_mock->mock("todoes", sub { return ($todo_mock_first,$todo_mock_second)});is($user_mock->get_all_todoes_memo_character_length, 8,"all_todoes_memo_character_length should be 8");
  • 8. モックオブジェクト DBD::Mocksub test_get_all_todoes_memo_character_length_with_dbd_mocck : Test { my $schema = Todo::Schema->connection(DBI:Mock:, , ); my $resultset = $schema->resultset(User); my $dbh = $schema->storage->dbh; my $results = [ [ "id", "name" ], [ "1", "oota" ], ]; $dbh->{mock_add_resultset} = $results; my ($oota) = $resultset->all; $results = [ ["id","user_id", "meno"], ["1", "1", "12345"], ["2", "1", "123"], ]; $dbh->{mock_add_resultset} = $results; is($oota->get_all_todoes_memo_character_length, 8, "all_todoes_memo_character_length should be 8 with DBD::Mock");}
  • 9. モックオブジェクトLLはダッグタイピングによりモックは作りやすいただし、逆に 本物のIFが変更されてもテストが通ってしまう コードとスキーマをできる限り分離させないDRYが 必要 Catalystならまあいいかな 理想はRuby on RailsのActiveRecord&DB migration
  • 10. テストごとのDB Test::mysqlsub startup : Test(startup) { my $self = shift; $self->{mysqld} = Test::mysqld->new( my_cnf => { skip-networking => , } ); $self->{schema} = Todo::Schema->connection($self->{mysqld}->dsn); $self->{schema}->deploy;}sub txn_helper { my ($self, $func) = @_; try { $self->{schema}->txn_begin; $func->(); } finally { $self->{schema}->txn_rollback; };}
  • 11. 独立したテスト Test::Classuse base Todo::DatabaseIntegrationTest;sub test_age : Test { my $self = shift; $self->txn_helper(sub { my $resultset = $self->{schema}->resultset(User); 略 is($oota->age($day_20130206), 37, "ootakens age is 37 on 2013/02/06"); });}sub test_get_all_todoes_memo_character_length : Test { my $self = shift; $self->txn_helper(sub { my $resultset = $self->{schema}->resultset(User); 略 is($oota->get_all_todoes_memo_character_length, 8, "all_todoes_memo_character_length should be 8"); });}
  • 12. テストごとのDB& 独立したテストテストの並列実行が可能になる prove –j nデータベースの生成コストを考慮する Not Test(setup) But Test(startup)スキーマのセットアップ Catalystなら Schema->deploy
  • 13. まとめTest::Perl::CriticTest::Perl::Metris::LiteTest::MockObjectDBD::MockTest::ClassTest::mysqld

×