Testing

Short version

piramid.png

We interested in

  • Unit tests
  • Intefration tests

TDD

tdd_lc.png

Learn

https://thoughtbot.com/upcase/fundamentals-of-tdd

TDLR

  • Write unit test: RED
  • Write realisation: GREEN
  • Refactor
  • Repeat

BDD

Behaviour-Driven Development (BDD) is a collaborative approach to software development that bridges the communication gap between business and IT.

BDD

BDD helps teams communicate requirements with more precision, discover defects early and produce software that remains maintainable over time.

BDD what?

  • One user story - one acceptance test: RED
  • Realisation: GREEN
  • Refactor -> repeat

How

https://thoughtbot.com/blog/back-to-basics-writing-unit-tests-first

In ruby

Rspec

In RSpec, tests are not just scripts that verify your application code. They’re also specifications (or specs, for short)

Old syntax

it 'example description' do
  Example.should be_truthy
end

New syntax

it 'example description' do
  expect(Example).to be_truthy
end

Matchers

https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers

Predicate matchers

describe 'PredicateMatchersExample' do
  class PredicateMatchersExample
    def has_predicate?
      true
    end
  end

  it 'have predicate' do
    predicate = PredicateMatchersExample.new
    expect(predicate).to have_predicate
  end
end

All matchers

describe 'all matchers' do
  it {expect(3,7,9).to all(be_odd)}
end

Change matchers

describe 'change matchers' do
  context 'array' do
    it 'change aray size' do
      list = [1,2,3,4]
      expect {list << 5}.to change(list, :size).from(4).to(5)
    end
  end
end

to let or not to let

  • let to define a memoized helper method. The value will be cached across multiple calls in the same example but not across examples.
  • let! to force invocation beetween calls

Doubles

https://relishapp.com/rspec/rspec-mocks/docs/basics/test-doubles

Doubles

it "returns canned responses from the methods named in the provided hash" do
  dbl = double("Some Collaborator", :foo => 3, :bar => 4)
  expect(dbl.foo).to eq(3)
  expect(dbl.bar).to eq(4)
end

Mock

describe "plugging in rspec" do
  it "allows rspec to be used" do
    target = mock('target')
    target.should_receive(:foo)
    target.foo
  end
end

Stub

# create a double
obj = double()

# stub a method
obj.stub(:message) # returns obj

# specify a return value
obj.stub(:message) { 'this is the value to return' }

Stub

https://relishapp.com/rspec/rspec-mocks/v/2-4/docs/method-stubs/stub-a-chain-of-methods

Guard

https://github.com/guard/guard

Guard example

https://github.com/voloyev/backend/blob/master/Guardfile

Minitest

https://github.com/seattlerb/minitest

There is rspec-like syntax

require "minitest/autorun"

describe Meme do
  before do
    @meme = Meme.new
  end

  describe "when asked about cheeseburgers" do
    it "must respond positively" do
      _(@meme.i_can_has_cheezburger?).must_equal "OHAI!"
    end
  end

  describe "when asked about blending possibilities" do
    it "won't say no" do
      _(@meme.will_it_blend?).wont_match /^no/i
    end
  end
end

Cucumber

https://cucumber.io/docs

Examples

https://cucumber.io/docs/guides/10-minute-tutorial/

Capybara

Acceptance test framework for web applications

Example

describe 'Setup addon', type: :feature do
  it 'setup addon' do
    visit('/en-US/account/login')

    within('fieldset') do
      fill_in('username', with: 'admin')
      fill_in('password', with: '<password>')
    end

    click_button 'Sign In'
    click_on('SAP Solman Technology Addon for Splunk ITSI')
    expect(page).to have_current_path('/en-US/app/sap_solman_app/')

    click_on('Settings')
    click_on('Indexes')
    click_on('New Index')

    fill_in('Index Name', with: 'solman')
    click_on('Metrics')
    click_on('Search & Reporting')
    click_on('SAP Solman Technology Addon for Splunk ITSI')
    click_on('Save')

    visit('/en-US/app/sap_solman_app/')

    click_on('Continue to app setup page')
    fill_in('sap__solman_base_url', with: ENV['SAP_URL'])
    fill_in('sap__username', with: ENV['SAP_USER'])
    fill_in('sap__solman_password', with: ENV['SAP_PASSWORD'])
    check('use_new_server_version')

    click_on('Save Configuration')

    sleep 30

    visit('/en-US/app/sap_solman_app/solman_configuration_dashboard')

    expect(page).to have_content('E77~ABAP')
    expect(page).to have_content('H83~HANADB')
    expect(page).to have_content('SLM~ABAP')
    expect(page).to have_content('E83~ABAP')
    expect(page).to have_content('SM6~ABAP')
  end
end

Add from splunk

Simplecov

simplecov.png

Simplecov

https://github.com/colszowka/simplecov

Lets get hands dirty

https://github.com/voloyev/cors_app