Testing Frameworks (MiniTest)



Similar documents
Ruby on Rails on Minitest

Rake Task Management Essentials

Survey of Unit-Testing Frameworks. by John Szakmeister and Tim Woods

Unit Testing. and. JUnit

Unit and Functional Testing for the ios Platform. Christopher M. Judd

PHPUnit Manual. Sebastian Bergmann

Unit Testing & JUnit

Evaluation. Chapter 1: An Overview Of Ruby Rails. Copy. 6) Static Pages Within a Rails Application

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Test Automation Integration with Test Management QAComplete

System Level Integration and Test Leveraging Software Unit Testing Techniques

Tools for Integration Testing

JUnit - A Whole Lot of Testing Going On

Author: Sascha Wolski Sebastian Hennebrueder Tutorials for Struts, EJB, xdoclet and eclipse.

JUnit Automated Software Testing Framework. Jeff Offutt. SWE 437 George Mason University Thanks in part to Aynur Abdurazik. What is JUnit?

Testing, Debugging, and Verification

Ruby On Rails A Cheatsheet. Ruby On Rails Commands

AP Computer Science Java Subset

Apache Thrift and Ruby

CIS 192: Lecture 13 Scientific Computing and Unit Testing

LINEAR INEQUALITIES. less than, < 2x + 5 x 3 less than or equal to, greater than, > 3x 2 x 6 greater than or equal to,

Approach of Unit testing with the help of JUnit

NewsletterAdmin 2.4 Setup Manual

Lab work Software Testing

PHPUnit Manual. Sebastian Bergmann

Performing Simple Calculations Using the Status Bar

Developing In Eclipse, with ADT

Effective unit testing with JUnit

UFTP AUTHENTICATION SERVICE

Software Engineering Techniques

Cucumber: Finishing the Example. CSCI 5828: Foundations of Software Engineering Lecture 23 04/09/2012

10 Listing data and basic command syntax

Moving from CS 61A Scheme to CS 61B Java

Invitation to Ezhil : A Tamil Programming Language for Early Computer-Science Education 07/10/13

The NetBeans TM Ruby IDE: You Thought Rails Development Was Fun Before

The Smalltalk Programming Language. Beatrice Åkerblom

Testing Python. Applying Unit Testing, TDD, BDD and Acceptance Testing

+ Introduction to JUnit. IT323 Software Engineering II By: Mashael Al-Duwais

Introduction to Computers and Programming. Testing

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Sorting. Lists have a sort method Strings are sorted alphabetically, except... Uppercase is sorted before lowercase (yes, strange)

Object Oriented Software Design

Table of Contents. LESSON: The JUnit Test Tool...1. Subjects...2. Testing What JUnit Provides...4. JUnit Concepts...5

Introduction to Programming System Design. CSCI 455x (4 Units)

Unit-testing with JML

Talend Component: tjasperreportexec

ECE 122. Engineering Problem Solving with Java

Writing Self-testing Java Classes with SelfTest

Part I. Multiple Choice Questions (2 points each):

UNIT TESTING. Written by Patrick Kua Oracle Australian Development Centre Oracle Corporation

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

A Puppet Approach To Application Deployment And Automation In Nokia. Oliver Hookins Principal Engineer Services & Developer Experience

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Performance Testing from User Perspective through Front End Software Testing Conference, 2013

Java Programming Fundamentals

Slides prepared by : Farzana Rahman TESTING WITH JUNIT IN ECLIPSE

Rectifier circuits & DC power supplies

Concepts and terminology in the Simula Programming Language

vcenter Orchestrator Developer's Guide

PHP Integration Kit. Version User Guide

Instance Creation. Chapter

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

The Cucumber Book. Extracted from: Behaviour-Driven Development for Testers and Developers. The Pragmatic Bookshelf

Web Applications Testing

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Tutorial IV: Unit Test

Introduction to Python for Text Analysis

Configuration Guide - OneDesk to SalesForce Connector

Creating a Java application using Perfect Developer and the Java Develo...

Hudson Continous Integration Server. Stefan Saasen,

Smarter Testing With Spock

Java Interview Questions and Answers

Test Case Design Techniques

Introduction to Web Services

Database Migration Plugin - Reference Documentation

by Jonathan Kohl and Paul Rogers 40 BETTER SOFTWARE APRIL

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

McAfee Cloud Identity Manager

Unit testing with JUnit and CPPUnit. Krzysztof Pietroszek

CSE 265: System and Network Administration. CSE 265: System and Network Administration

Licensed for viewing only. Printing is prohibited. For hard copies, please purchase from

McAfee Cloud Identity Manager

Lecture 2 Notes: Flow of Control

Java course - IAG0040. Unit testing & Agile Software Development

Test case design techniques I: Whitebox testing CISS

Java SE 8 Programming

TypeScript for C# developers. Making JavaScript manageable

An automatic predictive datamining tool. Data Preparation Propensity to Buy v1.05

Iteration CHAPTER 6. Topic Summary

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Transcription:

Testing Frameworks (MiniTest) Computer Science and Engineering College of Engineering The Ohio State University Lecture 32

MiniTest and RSpec Many popular testing libraries for Ruby MiniTest (replaces older Test::Unit) Comes built-in Looks like JUnit (mapped to Ruby syntax) Well-named! RSpec Installed as a library (i.e., a "gem") Looks different from JUnit (and even Ruby!) Most unfortunate name! RSpec view is that test cases define expected behavior i.e., they are the spec! What is wrong with that?

Writing MiniTest Tests Require runner and UUT require 'minitest/autorun' require 'card' Test fixture = subclass of Minitest::Test class TestCard < Minitest::Test Test case = method in the fixture Method name must begin with test_ def test_identifies_set Contains assertion(s) exercising a single piece of code / behavior / functionality Should be small (i.e., test one thing) Should be indepent (i.e., of other tests) Test Suite = collection of fixtures

Example: test_card.rb require 'minitest/autorun' require 'card' # assume card.rb on load path class TestCard < Minitest::Test def test_has_number assert_respond_to Card.new, :number def test_remembers_number @card = Card.new 1, "oval", "open", "red" assert_equal 1, @card.number

Execution Model TestCard instance of instance of @card has_number() remembers() has_number() remembers()

Execution Model: Implications Separate instances of test class created One instance / test case Test cases don't have side effects Passing/failing one test does not affect others Can not rely on order of tests Randomized order of execution Controllable with --seed command-line option Also controllable by invoking i_suck_and_my_tests_are_order_depent! Fixture: common set-up to all test cases Field(s) for instance(s) of class being tested Factor initialization code into its own method This method must be called setup

Good Practice: setup Initialize a fixture with a setup method (rather than initialize method) Reasons: If the code being tested throws an exception during the setup, the output is much more meaningful Symmetry with teardown method for cleaning up after a test case

Example: test_card.rb require 'minitest/autorun' require 'card' #assume card.rb is on load path class TestCard < Minitest::Test def setup @card = Card.new 1, "oval", "open", "red" def test_has_number assert_respond_to @card, :number def test_remembers_number assert_equal 1, @card.number

Execution Model TestCard instance of instance of 1 2 @card setup() has_number() remembers() 1 2 @card setup() has_number() remembers()

MiniTest Assertion Methods Most have two versions: assert & refute Example: assert_nil, refute_nil No need for negation (use refute instead) Most take an optional message assert_empty Library.new, "A new library contains no books" Message appears when assertion fails Specials: pass/flunk always passes/fails skip skips the rest of the test case Performance benchmarking also available

Common Assertions Boolean condition: assert/refute assert @books.all { b b.available?} Is nil: (assert/refute)_nil refute_nil @library.manager Is empty: (assert/refute)_empty assert_empty Library.new

Asserting Equality Assert two objects are == equal assert_equal expected, actual Compares object values (i.e., == in Ruby) Failure produces useful output TestCard#test_total_number_of_cards Expected: 81 Actual: 27 Compare with basic assert exp == actual TestCard#test_shuffle_is_permutation Failed assertion, no message given Two objects are the same (aliases) assert_same @table.north, @players.first Compares reference values (i.e.,.equal?)

More Assertions String matches a regular expression assert_match /CSE.*/, @course.name Collection includes a particular item assert_includes @library, @book Object is of a particular type assert_instance_of String, @book.title Object has a method assert_respond_to @student, :alarm Block raises an exception assert_raises ZeroDivisionError do @library.average_book_cost

Good Practice: Comparing Floats Never compare floating point numbers directly for equality assert_equal 1.456, calculated, Low-density experiment Numeric instabilities make exact equality problematic for floats Better approach: Equality with tolerance (delta or epsilon) assert_in_delta Math::PI, (22.0 / 7.0), 0.01, Archimedes algorithm" Delta for error, epsilon for relative error

Good Practice: Organization Keep tests in the same project as the code They are part of the build, the repo, etc. Helps to keep tests current Separate tests and implementation \set\lib contains card.rb \set\tests contains test_card.rb Name test classes consistently TestCard tests Card Test fixture is a Ruby program $ ruby tests\test_card.rb Test needs to be able to find UUT (require) Add location of UUT to load path $ ruby I lib tests\test_card.rb

Alternative Syntax Problem: cumbersome method names test_shuffle_changes_deck_configuration Solution: exploit Ruby language flexibility in API of testing library Methods are available that change the syntax and structure of test cases "Domain-specific language" for testing Result: MiniTest::Spec Notation inspired by Rspec

Writing MiniTest::Spec Tests Require spec library (+ runner + UUT) require 'minitest/spec Test fixture (an example group ) is a describe block describe Card do Can be nested, and identified by string The block contains examples Test case (an example ) is an it block it identifies a set Contains expectation(s) on a single piece of code / behavior / functionality Expectations are methods on objects @card.number.must_equal 1

Example: test_card.rb require 'minitest/spec require 'minitest/autorunner' require 'card' #assume card.rb is on load path describe Card, 'card for game of set' do it 'has a number' do Card.new.must_respond_to :number it 'remembers its original number' do @card = Card.new 1, "oval", "open", "red" @card.number.must_equal 1

Expectations vs. Assertions Positive and negative form must_be_empty wont_be_empty Have corresponding assertions Different argument order assert_equal expected, actual actual.must_equal expected No string argument Meaningful output comes from group and example name(s)

Obj.must_ + be x.must_be :<, 10 equal, be_same_as x.must_equal y be_nil be_empty, include be_instance_of, be_kind_of be_within_delta, be_within_epsilon match msg.must_match /^Dear/ respond_to raise output, be_silent

Setup/Teardown Methods before, after Arguments :each or :all describe Student do before :each do @buck_id = BuckID.new "4328429" @s = Student.new buck_id it 'should come to class' do

let: Lazy Initialization describe Student do let(:student) {Student.new 1234} describe 'sleep deprivation' it 'misses class' do student.awake?.must_equal false

Rspec: Set up and Use Install the rspec gem locally [~] $ gem install rspec Set up your program to use rspec [myapp] $ rspec -init Init creates several things in myapp/ spec/ # put tests (foo_spec.rb) here spec/spec_helper.rb # configures paths.rspec # default command-line args Run tests [myapp] $ rspec spec/foo_spec.rb

Example Groups and Examples require_relative '../student' describe Student do #example group it 'can drop a class' do #example... context 'when atting lecture' do it 'stays awake during lecture' do... it 'stores info until exam' do...

RSpec Expectations Verb is should (or should_not) target.should condition #notice space Examples of condition ==, equal factor.should equal 34 be_true, be_false, be_nil, be_empty list.emtpy?.should be_true have(n).items, have_at_most(n).items include(item) list.should include(name) match(regex) respond_to(method_name) New form: expect().to (or not_to) expect(a_result).to eq "OSU"

Stubs Top-down: testing a class that uses A, B, C We don't have A, B, C, we want quick approximations of A, B, C Behave in certain way, returning canned answers Stub method Takes a hash { method_names: return_values } Returns an object with those methods stub_printer = stub :available? => true, :rer => nil Another form adds (or changes) a method/return value to an existing object long_str = 'something' long_str.stub! (:length).and_return(1000000)

Mocks Stubs passively allow the test to go through Mocks monitor how they are used (and will fail if they aren't used right) it 'should know how to print itself' do mock_printer = mock('printer') mock_printer.should_receive (:available?).and_return(true) mock_printer.should_receive (:rer).exactly(3).times @doc.print (mock_printer).should == 'Done'

Summary MiniTest Test case: method named test_ Test fixture: class exting Minitest::Test Execution model: multiple instances Indepence of test cases MiniTest::Spec Examples and expectations String descriptions RSpec Stubs and mocks