22
Testing

Testing

Embed Size (px)

Citation preview

Page 1: Testing

Testing

Page 2: Testing

Test Driven Development

• Implementation

• Red-Green-Refactor

• Regression Tests

Page 3: Testing

Implementation

• Test what should happen

• Test what should not happen

Page 4: Testing

Red-Green-Refactor

Add a test

Run all tests

Write some code

Run the tests again

Refactor code

Page 5: Testing

Regression Tests

Is everything still working

Change environment

Change code

Page 6: Testing

Testing in php

$this->get("http://myserver/login.php");$this->assertWantedPattern("Please login to continue");

$this->setField("username", "MyTestUser");$this->setField("password", "t0ps3cr3t");$this->clickSubmit("Login");

$this->assertWantedPattern("You are logged in");

Page 7: Testing

Testing in Java

beginAt("login.jsp");assertTextInElement("h1", "Please login to continue");

setFormElement("username", "MyTestUser");setFormElement("password", "t0ps3cr3t");submit();

assertTextInElement("h1", "You are logged in");

Page 8: Testing

Testing in Rails

visit login_pathassert_contain "Please login to continue"

fill_in "username", :with => "MyTestUser"fill_in "password", :with => "t0ps3cr3t"click_button "Login"

assert_contain "You are logged in"

Page 9: Testing

Test Types

• Unit

• Functional

• Integration

Page 10: Testing

Unit

• Most basic level of testing

• Model tests in Rails

Page 11: Testing

Unit Testing

class Ship attr_accessor :captain attr_accessor :maties

def crew captain + maties endend

class ShipTest def test_crew

ship = Ship.newship.captain = 1ship.mateys = 20

assert_equal 21, ship.crew endend

Page 12: Testing

Functional

• Test lifecycle of objects

• Controller tests in Rails

Page 13: Testing

Functional Testingclass ShipsController def enter_other_ship @ship = Ship.find_captain(params[:captain]) @ship.gold += 1000 @ship.save redirect_to :action => 'show_loot' endend class ShipsControllerTest

def test_entering_other_ship post 'enter_other_ship', :captain => "Sparrow" assert_response :redirect assert_equal "Black Pearl", assign(:ship).name assert_equal 1000, assigns(:ship).gold endend

Page 14: Testing

Integration

• Overall application functionalities

• Walk through a series of events

• View Tests / Stories

Page 15: Testing

Integration Testing

Story: Attacking other ships As Captain Sparrow I attack another ship So I can buy more rum Scenario: Attack first ship Given another ship And my ship And my 20 mateys When we attack the other ship Then 1 pirate dies And we steel 1000 pieces of gold

def test_attacking_other_ships Given "another ship" do |ship| @ship_to_enter = Ship.find(ship) end Given "my ship" do @my_ship = Ship.find_by_captain("Sparrow") end Given /my $number_of_mateys maties/ do |number_of_mateys| @ship_to_enter.attackers = number_of_mateys end When "we attach the other ship" do @my_ship.attacks(@ship_to_enter) end Then /$pirates_lost pirate dies/ do |pirates_lost| assert_equal @my_schip.crew - pirates_lost, @ship_to_enter.attackers end Then /we steel $pieces_of_gold pieces of gold/ do |pieces_of_gold| assert_equal pieces_of_gold, @my_ship.gold endend

Page 16: Testing

Integration TestingStory: Attacking other ships As Captain Sparrow I attack another ship So I can buy more rum Scenario: Attack first ship Given another ship And my ship And my 20 mateys When we attack the other ship Then 1 pirate dies And we steel 1000 pieces of gold

Page 17: Testing

def test_attacking_other_ships Given "another ship" do |ship| @ship_to_enter = Ship.find(ship) end Given "my ship" do @my_ship = Ship.find_by_captain("Sparrow") end Given /my $number_of_mateys maties/ do |number_of_mateys| @ship_to_enter.attackers = number_of_mateys end When "we attach the other ship" do @my_ship.attacks(@ship_to_enter) end Then /$pirates_lost pirate dies/ do |pirates_lost| assert_equal @my_schip.crew - pirates_lost, @ship_to_enter.attackers end Then /we steel $pieces_of_gold pieces of gold/ do |pieces_of_gold| assert_equal pieces_of_gold, @my_ship.gold endend

Page 18: Testing

Test Data

• Mock/stub

• Fixtures

• Factories

Page 19: Testing

Mocking & Stubbing

- Mocha- Rspec- Flex Mock

def test_getting_tweet_on_homepage response = mock response.stubs(:authorization).returns(true) response.stubs(:last_tweet).resturns("#arrrrcamp rocks") TwitterAPI.expects(:get_last_tweet).returns(response) get 'index' assert_equal "#arrrrcamp rocks", assigns(:tweet).body end

Page 20: Testing

Fixturespirates.yml

captain_jack_sparrow: name: Jack Sparrow enemy: Royal Navy

ships.yml

black_pearl: name: The Black Pearl max_crew: 85 captain: captain_jack_sparrow

interceptor: name: The Interceptor max_crew: 150 captain: captain_jack_sparrow

Page 21: Testing

FactoriesFactory.sequence :pirate do |n| "matey#{n}"end

Factory.define :ship do |f| f.name 'Pirateship' f.max_crew 100 f.captain { Factory.next(:pirate) }end

should "only find big ships" do Factory(:ship, :max_crew => 500) Factory(:ship, :max_crew => 200)

ships = Ship.big_ones assert_equal 1, ships.sizeend

- Factory Girl- Machinist- Object Daddy- Foundry- Fixjour

Page 22: Testing

Questions