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

Minitest: voltando ao básico sobre testes

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

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

Transcript

91 slides
  1. …

  2. $ 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 -------------------------------------------------------------------------------
  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
  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 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 Caso de teste
  6. 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
  7. 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
  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
  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 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 Caso de teste
  11. 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
  12. class JSONSerializerTest < MiniTest::Test include SerializerTests def serializer JSON end

    end class MarshalSerializerTest < MiniTest::Test include SerializerTests def serializer Marshal end end Testes compartilhados
  13. # 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
  14. 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
  15. 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
  16. class BugTest < Minitest::Test def test_create_bang post = Post.create! assert_predicate

    post, :persisted? assert_equal 1, post.id end end executando
  17. 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
  18. # 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
  19. assert post.published? # Expected nil to be truthy. assert_predicate post,

    :published? # Expected #<Post:0x007f8fdc0939b8> to be published?. Minitest::Assertions
  20. # 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
  21. 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
  22. 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
  23. 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
  24. 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')
  25. 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
  26. 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 }
  27. “…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
  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. 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 (˽°□°҂˽Ɨ ˍ━ˍ
  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 end
  34. ✨

  35. 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
  36. 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()); });
  37. 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

More Decks by Lucas Mazza

Other Decks in Programming