+ All Categories
Home > Documents > (Advanced) Web Application Development

(Advanced) Web Application Development

Date post: 25-Feb-2016
Category:
Upload: shadi
View: 42 times
Download: 0 times
Share this document with a friend
Description:
(Advanced) Web Application Development. Test Driven Development with Ruby and Rails . Why do we bother testing?. When should we do testing?. A simple testing game. Three one minute rounds Done in pairs: pick a developer & a tester You might need to move seats. - PowerPoint PPT Presentation
Popular Tags:
37
(Advanced) Web Application Development Test Driven Development with Ruby and Rails Bruce Scharlau, University of Aberdeen, 2015
Transcript
Page 1: (Advanced) Web Application Development

(Advanced) Web Application Development

Test Driven Development with Ruby and Rails

Bruce Scharlau, University of Aberdeen, 2015

Page 2: (Advanced) Web Application Development

Why do we bother testing?

Bruce Scharlau, University of Aberdeen, 2015

Page 3: (Advanced) Web Application Development

Bruce Scharlau, University of Aberdeen, 2015

When should we do testing?

Page 4: (Advanced) Web Application Development

A simple testing game

Bruce Scharlau, University of Aberdeen, 2015

Game details at http://homepages.abdn.ac.uk/b.scharlau/pages/blog/?p=390

Three one minute roundsDone in pairs: pick a developer & a testerYou might need to move seats

Page 5: (Advanced) Web Application Development

Outline for this lecture

• Testing application options with Rails built-in tools

• Unit testing rails• Controller testing rails

Bruce Scharlau, University of Aberdeen, 2015

http://guides.rubyonrails.org/testing.html

Use basic guide on testing with methods, etc

Page 6: (Advanced) Web Application Development

Use agile development process“Working software over comprehensive documentation”

“Deliver working software frequently, from a couple of weeks to a couple of months, with a preference to the shorter timescale.”

Bruce Scharlau, University of Aberdeen, 2015

http://www.agilemanifesto.org

http://www.scrumguides.org/scrum-guide.html

Page 7: (Advanced) Web Application Development

Agile development focuses on working software in small batches

Bruce Scharlau, University of Aberdeen, 2015

14 days

Page 8: (Advanced) Web Application Development

Testing is part of the agile approach

• Ensures the code does what client requests

• Can be changed in line with client needs• Can be automated instead of manual

Bruce Scharlau, University of Aberdeen, 2015

Page 9: (Advanced) Web Application Development

You must test your appIf you don’t test your app, then you don’t know a) Either when you break it with new featuresb) Or which specific part broke

Bruce Scharlau, University of Aberdeen, 2015

Tests will tell you this information

If you don’t make the time now, then you’ll make the time after your app breaks. And it will break… You choose when to do tests

Page 10: (Advanced) Web Application Development

Agile provides better feedback

Bruce Scharlau, University of Aberdeen, 2015http://www.agilemodeling.com/essays/costOfChange.htm

Page 11: (Advanced) Web Application Development

Shortening feedback loop saves money

• Finding problems sooner, means they are fixed sooner

• Fixed sooner costs less money• Fixed sooner means less time spent on

them• Fixed sooner means more time for other

things, such as deploying app, and bringing in revenue

Bruce Scharlau, University of Aberdeen, 2015

Page 12: (Advanced) Web Application Development

Follow the TDD principles

Bruce Scharlau, University of Aberdeen, 2015http://en.wikipedia.org/wiki/File:Test-driven_development.PNG

Page 13: (Advanced) Web Application Development

Use red, green, refactor to code

Bruce Scharlau, University of Aberdeen, 2015http://patrickwilsonwelsh.com/?p=619

1. Write a little test

3. Get test to pass 2. Stub out code.Watch test fail

4. Refactor

Cycle time < 10 minutes

Make it green, then make it clean

Page 14: (Advanced) Web Application Development

Tests based on customer’s business needs

• Only write tests for what’s needed in app• This includes acceptance tests co-written

by customer – avoids misinterpretation

Bruce Scharlau, University of Aberdeen, 2015

only code what’s needed, then stop, move to next feature

Page 15: (Advanced) Web Application Development

Marick’s matrix of testing

Bruce Scharlau, University of Aberdeen, 2015

Imag

e fro

m h

ttp://

stop

andf

ix.b

logs

pot.c

o.uk

/200

9/04

/all-

abou

t-tes

ting.

htm

l

Bse

d on

idea

s fro

m h

ttp://

ww

w.e

xam

pler

.com

/old

-blo

g/20

03/0

8/21

/

Page 16: (Advanced) Web Application Development

There is no one tool for testing• Need to use a variety of tests • Unit test logical unit (class, method, etc) in

isolation• Integration test for components• Functional tests for end-to-end• System tests using same interface as

users• System integration for collaborating apps• Performance tests to ensure throughput

Bruce Scharlau, University of Aberdeen, 2015

Page 17: (Advanced) Web Application Development

Ruby provides various approaches to testing

• Cucumber and RSpec confirms methods conform to expected behaviour of code

• Test::Unit confirms methods work as expected

Bruce Scharlau, University of Aberdeen, 2015

Page 18: (Advanced) Web Application Development

Bruce Scharlau, University of Aberdeen, 2015

a) create cookbook: rails new cookbook then cd into cookbook and bundle install

b) setup migrations with rake db:createc) setup recipe model

rails generate scaffold recipe title:string description:string date:date instructions:textd) rake db:migrate to push migration to db servere) rails server to start server (don’t need to really though)f) rake test:units to setup test db

open test/unit/recipe_test.rb to write tests and rung) open fixtures file and edit for datah) open functional test and fix :one for :hot or :cold and run with rake test:functionalsi) test view in functional: assert_select 'h1', "Listing recipes”j) run rake:tests to do all again as group

We’ll use the cookbook for testing

Page 19: (Advanced) Web Application Development

Rails creates tests for us

Bruce Scharlau, University of Aberdeen, 2015

We just need to fill in code to use them

Tests automatically created

Page 20: (Advanced) Web Application Development

We have a variety of tests available

Bruce Scharlau, University of Aberdeen, 2015

We just need to fill in code to use them

Tests for models

Tests for controllers

Page 21: (Advanced) Web Application Development

We first check the test framework

Bruce Scharlau, University of Aberdeen, 2015

Create test database if doesn’t exist

Test database defined in database.ymlruby test/unit/recipe_test.rb

Page 22: (Advanced) Web Application Development

Need to prepare system with rake

Bruce Scharlau, University of Aberdeen, 2015

Run ‘rake test:units’ to copy schema to test db and run tests

Only works if you’ve used migrations, otherwise need to save table structure and load that into test db

Page 23: (Advanced) Web Application Development

We can use simple unit test code

Bruce Scharlau, University of Aberdeen, 2015

Default test, delete

Could also do validation checks

Might need to change ‘require’ linehttp://api.rubyonrails.org/ ActiveSupport::TestCase and Testing for details and examples

test “recipe test” do

Page 24: (Advanced) Web Application Development

Add more tests for other models

Bruce Scharlau, University of Aberdeen, 2015

As add models to app, you write tests

Keep checking the methods work

Can run tests independent of server, so speeds up working application

Page 25: (Advanced) Web Application Development

Use fixtures to load test data

Bruce Scharlau, University of Aberdeen, 2015

Provides continuity of tests

Can have one for each scenario

One place for data instead of being in test file

Page 26: (Advanced) Web Application Development

Fixture code in test is cleaner

Bruce Scharlau, University of Aberdeen, 2015

Reference fixture names and values

Load fixture file – must be same as table name

Can use fixture repeatedly in multiple tests

recipe.save will put it into db

Page 27: (Advanced) Web Application Development

Bruce Scharlau, University of Aberdeen, 2015

We can also test the controllersNeed to change ‘require’ line when run without rake

Change default of :one to :hot from fixture file

Run as is to check setup

Run with ‘rake test:functionals’

Page 28: (Advanced) Web Application Development

Test the controller outside server

Bruce Scharlau, University of Aberdeen, 2015

Change default of :one to :hot from fixture file or you will have error

One dot per test with ruby test…

Page 29: (Advanced) Web Application Development

We can write better tests

Bruce Scharlau, University of Aberdeen, 2015

Basic controller tests 7 methods of controller

Add new tests for methods you add

Page 30: (Advanced) Web Application Development

Test views from within controllers

Bruce Scharlau, University of Aberdeen, 2015

Select element, declare value

Page 31: (Advanced) Web Application Development

We can run all tests together

Bruce Scharlau, University of Aberdeen, 2015

We can run all tests at once as app grows

Page 32: (Advanced) Web Application Development

Bruce Scharlau, University of Aberdeen, 2015

Why do we do testing?

Page 33: (Advanced) Web Application Development

We can test the parts of the application as we write them

Bruce Scharlau, University of Aberdeen, 2015

We know the code works before we start server

We know code will be checked as we write new code

Provides a safety net for our coding: mistakes will be caught and fixed sooner

Page 34: (Advanced) Web Application Development

Combine your testing to cover whole application

You can use unit testing with Test::Unit, to test your whole Rails application

Details about this in the Agile Web Development book

Bruce Scharlau, University of Aberdeen, 2015

Page 35: (Advanced) Web Application Development

Bruce Scharlau, University of Aberdeen, 2015

When should we do testing?

Page 36: (Advanced) Web Application Development

There are still some parts missing from the tests

• We can add integration tests from Rails to mix controllers, etc

• We can also use Rspec and Cucumber to good effect to test the whole app

• Cucumber also tests web pages for us too

Bruce Scharlau, University of Aberdeen, 2015

We’ll look at Rspec and Cucumber next week

Page 37: (Advanced) Web Application Development

Summary• Use agile approach to development• Use unit tests for the models• Use functional tests for the controllers• Use ‘rake test’ to run all tests

Bruce Scharlau, University of Aberdeen, 2015

Tests provide a safety net for our coding

Testing is a big part of the Ruby community


Recommended