EUnit a powerful unit testing framework for Erlang Richard Carlsson Mickaël Rémond.

Slides:



Advertisements
Similar presentations
Copyright 2008 Sun Microsystems, Inc Better Expressiveness for HTM using Split Hardware Transactions Yossi Lev Brown University & Sun Microsystems Laboratories.
Advertisements

Unit Testing Australian Development Centre Brisbane, Australia.
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
What is Unit Testing? How TDD Works? Tsvyatko Konov Telerik Corporation
The Assembly Language Level
GoogleTest Primer. Outline Basic Concepts Assertions Basic Assertions Binary Comparison String Comparison Floating-Point Comparison Simple Tests Test.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
Introduction to Programming Lesson 1. Objectives Skills/ConceptsMTA Exam Objectives Understanding Computer Programming Understand computer storage and.
Unit and Functional Testing with JUnit and Related Tools Greg Barnes University of Washington
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
Chapter 11 Exception Handling and Event Handling.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Guide To UNIX Using Linux Third Edition
Adding Automated Functionality to Office Applications.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
© Dr. A. Williams, Fall Present Software Quality Assurance – JUnit Lab 1 JUnit A unit test framework for Java –Authors: Erich Gamma, Kent Beck Objective:
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Chapter 12: Exception Handling
© 2012 IBM Corporation Rational Insight | Back to Basis Series Chao Zhang Unit Testing.
Principles of Object Oriented Programming Practical session 2 – part A.
Tutorial 11 Using and Writing Visual Basic for Applications Code
1 v1.6 08/02/2006 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Refactoring 5.Debugging 6.Testing.
Testing in Extreme Programming
Unit and Functional Testing Your Flex Applications Mike Nimer Dir. Of Engineering nomee.com.
Tutorial 111 The Visual Studio.NET Environment The major differences between Visual Basic 6.0 and Visual Basic.NET are the latter’s support for true object-oriented.
Software Engineering 1 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring.
Introduction and Features of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++
Oracle Data Integrator Procedures, Advanced Workflows.
BIT 142:Programming & Data Structures in C#. What is Unit Testing? 2.
1.  Writing snippets of code that try to use methods (functions) from your program.  Each snippet should test one (and only one) function......by calling.
CSC 395 – Software Engineering Lecture 10: Execution-based Testing –or– We can make it better than it was. Better...faster...agiler.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
1 BUILDING JAVA PROGRAMS CHAPTER 6 FILE PROCESSING.
First BlueJ Day Houston, 2006 Unit Testing with BlueJ Bruce Quig Deakin University.
Unit Testing Maintaining Quality. How do you test? Testing to date…
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
Test Specifications A Specification System for Multi-Platform Test Suite Configuration, Build, and Execution Greg Cooksey.
EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group.
JUnit Don Braffitt Updated: 10-Jun-2011.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
PROGRAMMING TESTING B MODULE 2: SOFTWARE SYSTEMS 22 NOVEMBER 2013.
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
The Development Process Compilation. Compilation - Dr. Craig A. Struble 2 Programming Process Problem Solving Phase We will spend significant time on.
Exception Handling in C + + Introduction Overview of C++ Exception Handling Designing With Exceptions Exception Handling Philosophies Conclusion.
Google C++ Testing Framework Part 2: Assertion. Concepts A test case contains one or many tests. ◦ You should group your tests into test cases that reflect.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
By Durga Dasari. DbFit - DB Unit and Integration test tool  Test-driven database development. Write readable, easy- to-maintain unit and integration.
Interstage BPM v11.2 1Copyright © 2010 FUJITSU LIMITED ADVANCE FEATURES.
Introduction to Programming Lesson 1. Algorithms Algorithm refers to a method for solving problems. Common techniques for representing an algorithms:
CSE 143 Lecture 14: testing.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Unit Testing.
Assembler, Compiler, MIPS simulator
Software Construction Lab 10 Unit Testing with JUnit
Introduction to JUnit CS 4501 / 6501 Software Testing
Coding Defensively Coding Defensively
Introduction to JUnit CS 4501 / 6501 Software Testing
More JUnit CS 4501 / 6501 Software Testing
Functional interface.
CS 240 – Advanced Programming Concepts
Java Statements B.Ramamurthy CS114A, CS504 4/23/2019 BR.
Introduction to Programming
Joel Adams and Jeremy Frens Calvin College
Server-Side Programming
Automated test.
Presentation transcript:

EUnit a powerful unit testing framework for Erlang Richard Carlsson Mickaël Rémond

What is unit testing? ● Testing “program units” in isolation  Functions, modules, subsystems ● Testing specified behaviour (contracts)  Input/output  Stimulus/response  Preconditions/postconditions, invariants

What unit testing is not ● Unit testing does not cover:  Performance testing  Usability testing  System testing  Etc. ● Unit tests do not replace, but can be an important part of:  Regression testing  Integration testing

Test-driven design ● Write unit tests (and run them, often) while developing the program, not afterwards ● Write tests for a feature before implementing that feature ● Move on to another feature when all tests pass ● Good for focus and productivity:  Concentrate on solving the right problems  Avoid overspecification and premature optimization ● Regression tests for free

Unit testing frameworks ● Make it easy to:  Write tests ● minimal typing overhead  Run tests ● at the push of a button  View test results ● efficient feedback ● Made popular by the JUnit framework for Java by Beck and Gamma.

Functional unit testing ● The “xUnit family” of frameworks (JUnit, etc.) are mostly built on object-oriented principles  Heavy use of inheritance: ● scaffolding (code for running the tests) ● setup/teardown of test contexts (open/close files, etc.) ● We want to be able to handle tests as data  Lambda expressions (funs) make natural test cases  Represent test sets as collections (lists) of funs  Deep lists make it easy to combine test sets

Test case conventions ● A test case is represented as a fun that takes no arguments ● A test fails if evaluation throws an exception ● Any return value is considered a success Test = fun () ->... end, try Test() of _ -> ok catch _:_ -> error end

Using EUnit ● Most basic usage: eunit:test(TestSet) ● Where TestSet can be:  a fun (taking zero arguments)  a module name  various other things (as we shall see)  a (deep) list of funs, module names, and other things

Modules as test sets ● Given a module name (any atom), EUnit will look for exported functions with special names:... _test()% simple test... _test_()% test generator ● Simple test functions are treated just as funs: they represent a single test case. ● Test generators are called immediately, and should return a test set (a fun, module name, list of tests, etc.)

Using EUnit in your code ● The hard way:  Add test functions (with conforming names) ● simple test functions (one test case per function) ● generator functions, returning lists of funs  Export the functions  Call eunit:test(Modulename) ● Using EUnit header files makes life easier  Many useful macros  Some additional magic

EUnit utility macros ● -include_lib(“eunit/include/eunit.hrl”). ● Make the code more compact and readable ● Generate more informative exceptions ?assert(1 + 1 == 2) ?assertMatch({foo, _}, make_foo(42)) ?assertError(badarith, 1/0) ?assertThrow(ouch, throw(ouch))

Fun-wrapper macros ● Macros whose names begin with “_ ” wrap their arguments in a fun-expression: ?_test(Expr) fun()-> Expr end ● Most test macros have _-forms: ?_test(?assert(BoolExpr)) ?_assert(BoolExpr) ● Usage comparison: simple_test()-> ?assert(BoolExpr). fun_test_()-> ?_assert(BoolExpr).

Developing a module with EUnit -module(fib). -export([fib/1]). fib(0) -> 1; fib(1) -> 1; fib(N) when N > 1 -> fib(N-1) * fib(N-2).

Developing a module with EUnit -module(fib). -export([fib/1]). -include_lib(“eunit/include/eunit.hrl”). fib(0) -> 1; fib(1) -> 1; fib(N) when N > 1 -> fib(N-1) * fib(N-2).

Test functions are auto-exported -module(fib). -export([fib/1]). -include_lib(“eunit/include/eunit.hrl”). fib(0) -> 1; fib(1) -> 1; fib(N) when N > 1 -> fib(N-1) * fib(N-2). fib0_test() -> ?assert(fib(0) == 1).

Test functions are auto-exported -module(fib). -export([fib/1]). -include_lib(“eunit/include/eunit.hrl”). fib(0) -> 1; fib(1) -> 1; fib(N) when N > 1 -> fib(N-1) * fib(N-2). fib0_test() -> ?assert(fib(0) == 1). fib1_test() -> ?assert(fib(1) == 1).

Test functions are auto-exported -module(fib). -export([fib/1]). -include_lib(“eunit/include/eunit.hrl”). fib(0) -> 1; fib(1) -> 1; fib(N) when N > 1 -> fib(N-1) * fib(N-2). fib0_test() -> ?assert(fib(0) == 1). fib1_test() -> ?assert(fib(1) == 1). fib2_test() -> ?assert(fib(2) == 2).

Compact code with generators -module(fib). -export([fib/1]). -include_lib(“eunit/include/eunit.hrl”). fib(0) -> 1; fib(1) -> 1; fib(N) when N > 1 -> fib(N-1) * fib(N-2). fib_test_() -> [?_assert(fib(0) == 1), ?_assert(fib(1) == 1), ?_assert(fib(2) == 2)].

Compact code with generators -module(fib). -export([fib/1]). -include_lib(“eunit/include/eunit.hrl”). fib(0) -> 1; fib(1) -> 1; fib(N) when N > 1 -> fib(N-1) * fib(N-2). fib_test_() -> [?_assert(fib(0) == 1), ?_assert(fib(1) == 1), ?_assert(fib(2) == 2), ?_assert(fib(3) == 3), ?_assert(fib(4) == 5), ?_assert(fib(5) == 8)].

Also test the error cases -module(fib). -export([fib/1]). -include_lib(“eunit/include/eunit.hrl”). fib(0) -> 1; fib(1) -> 1; fib(N) when N > 1 -> fib(N-1) * fib(N-2). fib_test_() -> [?_assert(fib(0) == 1), ?_assert(fib(1) == 1), ?_assert(fib(2) == 2), ?_assert(fib(3) == 3), ?_assert(fib(4) == 5), ?_assert(fib(5) == 8), ?_assertError(function_clause, fib(-1))].

Compiling and running 1> c(fib).

Compiling and running 1> c(fib). {ok,fib} 2>

Compiling and running 1> c(fib). {ok,fib} 2> eunit:test(fib).

Reading error reports 1> c(fib). {ok,fib} 2> eunit:test(fib). fib:12:fib_test_...*failed* ::{error,{assertion_failed, [{module, fib}, {line,12, {expression, “fib ( 2 ) == 2”}, {expected, true}, {value, false}]},... ================================================= Failed: 4 Aborted: 0 Skipped: 0 Succeeded: 3 3>

Fixing errors -module(fib). -export([fib/1]). -include_lib(“eunit/include/eunit.hrl”). fib(0) -> 1; fib(1) -> 1; fib(N) when N > 1 -> fib(N-1) * fib(N-2). fib_test() -> [?_assert(fib(0) == 1), ?_assert(fib(1) == 1), ?_assert(fib(2) == 2), ?_assert(fib(3) == 3), ?_assert(fib(4) == 5), ?_assert(fib(5) == 8), ?_assertError(function_clause, fib(-1))].

Fixing errors -module(fib). -export([fib/1]). -include_lib(“eunit/include/eunit.hrl”). fib(0) -> 1; fib(1) -> 1; fib(N) when N > 1 -> fib(N-1) + fib(N-2). fib_test() -> [?_assert(fib(0) == 1), ?_assert(fib(1) == 1), ?_assert(fib(2) == 2), ?_assert(fib(3) == 3), ?_assert(fib(4) == 5), ?_assert(fib(5) == 8), ?_assertError(function_clause, fib(-1))].

Using the automatic test() function 3> c(fib). {ok,fib} 4> fib:test().

Using the automatic test() function 3> c(fib). {ok,fib} 4> fib:test(). All 7 tests successful ok 5>

Advanced test descriptors ● Labels {“Label Text”, Tests}

Advanced test descriptors ● Labels {“Label Text”, Tests} ● Timeouts {timeout, Seconds, Tests}

Advanced test descriptors ● Labels {“Label Text”, Tests} ● Timeouts {timeout, Seconds, Tests} ● Running tests in subprocesses {spawn, Tests}

Advanced test descriptors ● Labels {“Label Text”, Tests} ● Timeouts {timeout, Seconds, Tests} ● Running tests in subprocesses {spawn, Tests} {spawn, Node, Tests}

Advanced test descriptors ● Labels {“Label Text”, Tests} ● Timeouts {timeout, Seconds, Tests} ● Running tests in subprocesses {spawn, Tests} {spawn, Node, Tests} ● Specifying the execution order {inparallel, Tests}

Advanced test descriptors ● Labels {“Label Text”, Tests} ● Timeouts {timeout, Seconds, Tests} ● Running tests in subprocesses {spawn, Tests} {spawn, Node, Tests} ● Specifying the execution order {inparallel, Tests} {inorder, Tests}

More advanced test descriptors ● Running system commands {cmd, “Command String”}

More advanced test descriptors ● Running system commands {cmd, “Command String”} ● Generators {generator, Fun} {generator, Module, Function}

More advanced test descriptors ● Running system commands {cmd, “Command String”} ● Generators {generator, Fun} {generator, Module, Function} ● Test setup and cleanup {setup, SetupFun, % () -> R::any() CleanupFun, % (R::any()) -> any() (Instantiator % (R::any()) -> Tests | Tests) } (setup runs the tests in a new process by default)

And some even more advanced ● Repeated setup and cleanup {foreach, SetupFun, CleanupFun, [ Instantiator|Tests ] } {foreachx, SetupFunX, CleanupFunX, [ {X::any(), InstantiatorX|Tests} ]

End