Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Minitest: voltando ao básico sobre testes

Minitest: voltando ao básico sobre testes

Minitest nasceu como uma alternativa ao test/unit do Ruby 1.8 e se tornou um framework de testes extremamente robusto e ao mesmo tempo simples, e está disponível em qualquer instalação de Ruby hoje em dia, para todos escrevem testes sem muita burocracia.

Lucas Mazza

July 23, 2016
Tweet

More Decks by Lucas Mazza

Other Decks in Programming

Transcript

  1. $ gem fetch minitest && gem unpack minitest-*.gem Downloaded minitest-5.9.0

    Unpacked gem: '/private/tmp/minitest-5.9.0' $ cloc minitest-5.9.0/lib 14 text files. 14 unique files. 0 files ignored. github.com/AlDanial/cloc v 1.68 T=0.09 s (159.9 files/s, 38368.5 lines/s) ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- Ruby 14 661 1072 1627 ------------------------------------------------------------------------------- SUM: 14 661 1072 1627 -------------------------------------------------------------------------------
  2. class PostTest < Minitest::Test def test_sets_the_title post = Post.new post.title

    = 'The Post Title' assert_equal 'The Post Title', post.title end def test_publish_publishes_the_post post = Post.new post.publish! assert_predicate post, :published? end end
  3. class PostTest < Minitest::Test def test_sets_the_title post = Post.new post.title

    = 'The Post Title' assert_equal 'The Post Title', post.title end def test_publish_publishes_the_post post = Post.new post.publish! assert_predicate post, :published? end end Caso de teste
  4. class PostTest < Minitest::Test def test_sets_the_title post = Post.new post.title

    = 'The Post Title' assert_equal 'The Post Title', post.title end def test_publish_publishes_the_post post = Post.new post.publish! assert_predicate post, :published? end end Teste Teste Caso de teste
  5. class PostTest < Minitest::Test def test_sets_the_title post = Post.new post.title

    = 'The Post Title' assert_equal 'The Post Title', post.title end def test_publish_publishes_the_post post = Post.new post.publish! assert_predicate post, :published? end end Teste Teste Asserção Asserção Caso de teste
  6. RSpec.describe Post do it '#title= sets the title' do post

    = Post.new post.title = 'The Post Title' expect(post.title).to eq('The Post Title') end it '#publish! publishes the post' do post = Post.new post.publish! expect(post).to be_published end end Teste Teste Asserção Asserção Caso de teste
  7. class PostSpec < RSpec::Core::ExampleGroup set_it_up 'Post', [], RSpec.world.example_groups RSpec::Core::Example.new(self, '#title=

    sets the title', {}, -> (example) { post = Post.new post.title = 'The Post Title' RSpec::Expectations::ExpectationTarget.new(post.title).to( RSpec::Matchers::BuiltIn::Eq.new('The Post Title')) }) RSpec::Core::Example.new(self, 'publish! publishes the post', {}, -> (example) { post = Post.new post.publish! RSpec::Expectations::ExpectationTarget.new(post).to( RSpec::Matchers::BuiltIn::BePredicate.new(:be_published)) }) end RSpec.world.record PostSpec
  8. class PostSpec < RSpec::Core::ExampleGroup set_it_up 'Post', [], RSpec.world.example_groups RSpec::Core::Example.new(self, '#title=

    sets the title', {}, -> (example) { post = Post.new post.title = 'The Post Title' RSpec::Expectations::ExpectationTarget.new(post.title).to( RSpec::Matchers::BuiltIn::Eq.new('The Post Title')) }) RSpec::Core::Example.new(self, 'publish! publishes the post', {}, -> (example) { post = Post.new post.publish! RSpec::Expectations::ExpectationTarget.new(post).to( RSpec::Matchers::BuiltIn::BePredicate.new(:be_published)) }) end RSpec.world.record PostSpec Caso de teste
  9. class PostSpec < RSpec::Core::ExampleGroup set_it_up 'Post', [], RSpec.world.example_groups RSpec::Core::Example.new(self, '#title=

    sets the title', {}, -> (example) { post = Post.new post.title = 'The Post Title' RSpec::Expectations::ExpectationTarget.new(post.title).to( RSpec::Matchers::BuiltIn::Eq.new('The Post Title')) }) RSpec::Core::Example.new(self, 'publish! publishes the post', {}, -> (example) { post = Post.new post.publish! RSpec::Expectations::ExpectationTarget.new(post).to( RSpec::Matchers::BuiltIn::BePredicate.new(:be_published)) }) end RSpec.world.record PostSpec Teste Teste Caso de teste
  10. class PostSpec < RSpec::Core::ExampleGroup set_it_up 'Post', [], RSpec.world.example_groups RSpec::Core::Example.new(self, '#title=

    sets the title', {}, -> (example) { post = Post.new post.title = 'The Post Title' RSpec::Expectations::ExpectationTarget.new(post.title).to( RSpec::Matchers::BuiltIn::Eq.new('The Post Title')) }) RSpec::Core::Example.new(self, 'publish! publishes the post', {}, -> (example) { post = Post.new post.publish! RSpec::Expectations::ExpectationTarget.new(post).to( RSpec::Matchers::BuiltIn::BePredicate.new(:be_published)) }) end RSpec.world.record PostSpec Teste Teste Asserção Asserção Caso de teste
  11. class JSONSerializerTest < MiniTest::Test include SerializerTests def serializer JSON end

    end class MarshalSerializerTest < MiniTest::Test include SerializerTests def serializer Marshal end end Testes compartilhados
  12. # activerecord/test/cases/base_test.rb class LintTest < ActiveRecord::TestCase include ActiveModel::Lint::Tests class LintModel

    < ActiveRecord::Base; end def setup @model = LintModel.new end end Testes compartilhados
  13. class Post attr_accessor :title def publish! @published = true end

    def published? @published end end require 'minitest/autorun' class PostTest < Minitest::Test def test_sets_the_title post = Post.new post.title = 'The Post Title' assert_equal 'The Post Title', post.title end def test_publish_publishes_the_post post = Post.new post.publish! assert_predicate post, :published? end end
  14. require 'active_record' require 'minitest/autorun' require 'logger' ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')

    ActiveRecord::Schema.define do create_table :posts, force: true do |t| end end class Post < ActiveRecord::Base end # ... executando
  15. class BugTest < Minitest::Test def test_create_bang post = Post.create! assert_predicate

    post, :persisted? assert_equal 1, post.id end end executando
  16. assert assert_empty assert_equal assert_in_delta assert_in_epsilon assert_includes assert_instance_of assert_kind_of assert_match assert_nil

    assert_operator assert_output assert_predicate assert_raises assert_respond_to assert_same assert_send assert_silent assert_throws Minitest::Assertions
  17. # lib/minitest/assertions.rb def assert_includes collection, obj, msg = nil msg

    = message(msg) { "Expected #{mu_pp(collection)} to include #{mu_pp(obj)}" } assert_respond_to collection, :include? assert collection.include?(obj), msg end Minitest::Assertions
  18. assert post.published? # Expected nil to be truthy. assert_predicate post,

    :published? # Expected #<Post:0x007f8fdc0939b8> to be published?. Minitest::Assertions
  19. # test/support/assertions.rb def refute_file(path, msg = nil) msg = message(msg)

    { "Expected '#{path}' to not exist." } refute File.exist?(path), msg end def assert_file(path, contents = nil, msg = nil) msg = message(msg) { "Expected '#{path}' to exist." } assert File.exist?(path), msg if contents.present? assert_equal contents, File.read(path) end end asserções
  20. fnando/minitest-utils require 'bundler/inline' gemfile(true) do gem 'minitest' gem 'minitest-utils', require:

    false end require 'minitest/autorun' class PluginTest < Minitest::Test def test_that_fails assert_equal 1, 2 end end
  21. require 'active_support' require 'active_support/test_case' class PostTest < ActiveSupport::TestCase test '#title=

    sets the title' do post = Post.new post.title = 'The Post Title' assert_equal 'The Post Title', post.title end test '#publish! publishes the post' do post = Post.new post.publish! assert_predicate post, :published? end end gem install activesupport
  22. require 'active_support' require 'active_support/test_case' class PostTest < ActiveSupport::TestCase setup :setup_something

    setup { setup_something_else } teardown :teardown_something end gem install activesupport
  23. mocks book = double('book') null_book = double('book').as_null_object book = instance_double('Book',

    pages: 250) allow(book).to receive_messages( title: 'The RSpec Book', pages: 250) invitation = spy('invitation')
  24. mocks require 'minitest/mock' book = Minitest::Mock.new book.expect(:pages, 250) book.expect(:title, 'The

    RSpec Book’) book.expect(:title, 'The RSpec Book’) book.expect(:update, true, [{ title: 'New Title’ }]) assert_mock book
  25. stubs allow(book).to receive_messages( title: 'The RSpec Book', pages: 250) expect(book).to

    receive_messages( title: 'The RSpec Book', pages: 250) allow_any_instance_of(Book).to receive(:update).with(hash_including(title: 'New Title')) { true }
  26. “…any pain in faking something should be responded to not

    with ever-more-clever test double libraries, but with a redesign of the interaction between the subject and its dependencies.” Justin Searls - Don’t mock what you don’t own https:/ /github.com/testdouble/contributing-tests/wiki/Don't-mock-what-you-don't-own
  27. RSpec.describe Post do subject { Post.new } let(:foo) { }

    let(:bar) { } describe '...' do before { } let(:bar) { } context '...' do before { } let(:foo) { } it { } it { } end context '...' do before { } let(:bar) { } it { } it { } it { } end end end
  28. RSpec.describe Post do subject { Post.new } let(:foo) { }

    let(:bar) { } describe '...' do before { } let(:bar) { } context '...' do before { } let(:foo) { } it { } it { } end context '...' do before { } let(:bar) { } it { } it { } it { } end end end
  29. RSpec.describe Post do subject { Post.new } let(:foo) { }

    let(:bar) { } describe '...' do before { } let(:bar) { } context '...' do before { } let(:foo) { } it { } it { } end context '...' do before { } let(:bar) { } it { } it { } it { } end end end
  30. RSpec.describe Post do subject { Post.new } let(:foo) { }

    let(:bar) { } describe '...' do before { } let(:bar) { } context '...' do before { } let(:foo) { } it { } it { } end context '...' do before { } let(:bar) { } it { } it { } it { } end end end
  31. RSpec.describe Post do subject { Post.new } let(:foo) { }

    let(:bar) { } describe '...' do before { } let(:bar) { } context '...' do before { } let(:foo) { } it { } it { } end context '...' do before { } let(:bar) { } it { } it { } it { } end end end (˽°□°҂˽Ɨ ˍ━ˍ
  32. class PostTest < Minitest::Test def test_sets_the_title post = Post.new post.title

    = 'The Post Title' assert_equal 'The Post Title', post.title end end
  33. class PostTest < Minitest::Test def test_sets_the_title post = Post.new post.title

    = 'The Post Title' assert_equal 'The Post Title', post.title end def test_publish_publishes_the_post post = Post.new post.publish! assert_predicate post, :published? end end
  34. QUnit.module('Post tests'); QUnit.test('sets the post title', function(assert) { var post

    = new Post(); post.set('title', 'The Post Title'); assert.equal('The Post Title', post.get('title')); }); QUnit.test('post is publishable', function(assert) { var post = new Post(); post.publish(); assert.ok(post.isPublished()); });
  35. defmodule PostTest do use ExUnit.Case test "sets the title" do

    post = %Post{} post = Map.put(post, :title, "The Post Title") assert "The Post Title" == post.title end test "publishes the post" do post = %Post{} post = Post.publish!(post) assert Post.published?(post) end end