• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Jenkinsableじゃないプロジェクトをjenkinsableにするまでperl編
 

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

on

  • 980 views

 

Statistics

Views

Total Views
980
Views on SlideShare
980
Embed Views
0

Actions

Likes
2
Downloads
6
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

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

    • Jenkinsableじゃないプロジェクトを JenkinsableにするまでPerl編 太田健一郎 @oota_ken
    • LLではコンパイルがな いJenkinsの最も分かりやすい導入効果であるコンパイルの自動実行がLLでは使えない自動テストがないプロジェクトで自動テストを導入するのは大変じゃ、どうすればいい?
    • 静的検証 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;
    • 静的検証 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;
    • 静的検証 on Unit Test組み込み簡単テストが失敗すると直してもらえる普通に静的検証 on Jenkinsより効果高いベストプラクティスが身につく
    • 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 );
    • モックオブジェクト 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");
    • モックオブジェクト 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");}
    • モックオブジェクトLLはダッグタイピングによりモックは作りやすいただし、逆に 本物のIFが変更されてもテストが通ってしまう コードとスキーマをできる限り分離させないDRYが 必要 Catalystならまあいいかな 理想はRuby on RailsのActiveRecord&DB migration
    • テストごとの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; };}
    • 独立したテスト 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"); });}
    • テストごとのDB& 独立したテストテストの並列実行が可能になる prove –j nデータベースの生成コストを考慮する Not Test(setup) But Test(startup)スキーマのセットアップ Catalystなら Schema->deploy
    • まとめTest::Perl::CriticTest::Perl::Metris::LiteTest::MockObjectDBD::MockTest::ClassTest::mysqld