PHPUnit Julia Bondar 040593 IAPM23. Agenda What is PHPUnit How to write an automated test Writing and running tests with PHPUnit Advantages and disadvantages.

Slides:



Advertisements
Similar presentations
Debugging ACL Scripts.
Advertisements

compilers and interpreters
Unit Testing Australian Development Centre Brisbane, Australia.
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
J-Unit Framework.
Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
T ESTING WITH J UNIT IN E CLIPSE Farzana Rahman. I NTRODUCTION The class that you will want to test is created first so that Eclipse will be able to find.
TEST-DRIVEN DEVELOPMENT Lecture 3. Definition Test-driven development (development through testing) is a technique of programming, in which the unit tests.
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
Well-behaved objects 4.0 Testing. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to.
JUnit. What is unit testing? A unit is the smallest testable part of an application. A unit test automatically verifies the correctness of the unit. There.
1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial.
JUnit, Revisited 17-Apr-17.
Fall 2007CS 2251 Programming Tools Eclipse JUnit Testing make and ant.
22-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
23-Jun-15 Unit Testing in Ruby. Programming methodologies The grim facts: The majority of large programming projects fail Projects that succeed are usually.
24-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
24-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
Writing a Unit test Using JUnit At the top of the file include: import junit.framework.TestCase; The main class of the file must be: public Must extend.
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
George Blank University Lecturer. JUnit for Test Driven Development By Vivek Bhagat, George Blank.
JsUnit An introduction to unit testing in-browser JavaScript with JsUnit Edward Hieatt February, 2005 JsUnit.
Programmer Testing Testing all things Java using JUnit and extensions.
© Dr. A. Williams, Fall Present Software Quality Assurance – JUnit Lab 1 JUnit A unit test framework for Java –Authors: Erich Gamma, Kent Beck Objective:
Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Software Development Tools COMP220 Seb Coope Week 8 Lecture 1 Ant, Testing and JUnit (2) These slides are mainly based on “Java Development with Ant” -
SENG 403, Winter 2012 SENG 403 – Winter  Exploring unit-testing frameworks in.NET  Writing our first test with NUnit  Unit Testing in Visual.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Lecture 6 Software Testing and jUnit CS140 Dick Steflik.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
CSCI 6962: Server-side Design and Programming Validation Tools in Java Server Faces.
ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.
CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.
CIT 590 Intro to Programming Lecture 4. Agenda Doubts from HW1 and HW2 Main function Break, quit, exit Function argument names, scope What is modularity!
Software Testing, Debugging and JUnit Ananda Gunawardena Hao Cen.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
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.
(1) Unit Testing and Test Planning CS2110: SW Development Methods These slides design for use in lab. They supplement more complete slides used in lecture.
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.
A tool for test-driven development
Week81 APCS-AB: Java Unit Testing Information today from “Unit Testing in BlueJ” October 28, 2005.
JUnit Don Braffitt Updated: 10-Jun-2011.
CSI 3125, Preliminaries, page 1 Compiling the Program.
JUnit. Introduction JUnit is an open source Java testing framework used to write and run repeatable tests JUnit is integrated with several IDEs, including.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be.
JUnit A Unit Testing Framework for Java. The Objective Introduce JUnit as a tool for Unit Testing Provide information on how to: Install it Build a test.
RTD Basic Training. Agenda Control PC Network Connection Setup Configure RTD –RTD License Installation –RTD Environment Configuration –Archives Basic.
Test Automation For Web-Based Applications Portnov Computer School 1 Selenium HP Web Test Tool Training.
Test a Little, Code a Little Colin Sharples IBM Global Services New Zealand Colin Sharples IBM Global Services New Zealand.
Unit Testing with FlexUnit
Justin Bare and Deric Pang with material from Erin Peach, Nick Carney, Vinod Rathnam, Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
OCR A Level F453: The function and purpose of translators Translators a. describe the need for, and use of, translators to convert source code.
Automated Testing with PHPUnit. How do you know your code works?
Getting Started with JUnit Getting Started with JUnit The benefits and ease of writing and running JUnit test cases and test suites. The benefits and ease.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
PHPUnit vs PHPSpec © Rudolf Horváth
Software Construction Lab 10 Unit Testing with JUnit
Don Braffitt Updated: 26-Mar-2013
JUnit Automated Software Testing Framework
Introduction to JUnit CS 4501 / 6501 Software Testing
Unit Testing in Ruby 22-Feb-19.
Joel Adams and Jeremy Frens Calvin College
Unit Testing.
Presentation transcript:

PHPUnit Julia Bondar IAPM23

Agenda What is PHPUnit How to write an automated test Writing and running tests with PHPUnit Advantages and disadvantages of PHPUnit

PHPUnit Testing with PHPUnit means checking that your program behaves as expected, and performing a battery of tests, runnable code- fragments that automatically test the correctness of parts (units) of the software. These runnable code-fragments are called unit tests.

Print-based test -> Automated test <?php $fixture = array(); // $fixture is expected to be empty. $fixture[] = 'element'; // $fixture is expected to contain one element. ?>

Print-based test -> Automated test <?php $fixture = array(); print sizeof($fixture). "\n"; $fixture[] = 'element'; print sizeof($fixture). "\n"; ?> 0101

Print-based test -> Automated test ok

Automated test <?php $fixture = array(); assertTrue(sizeof($fixture) == 0); $fixture[] = 'element'; assertTrue(sizeof($fixture) == 1); function assertTrue($condition) { if (!$condition) { throw new Exception('Assertion failed.'); } } ?>

Writing Tests with PHPUnit assertEquals(0, sizeof($fixture)); } public function testArrayContainsAnElement() { // Create the Array fixture. $fixture = array(); // Add an element to the Array fixture. $fixture[] = 'Element'; // Assert that the size of the Array fixture is 1. $this->assertEquals(1, sizeof($fixture)); } } ?>

Test class should extend the class PHPUnit_Framework_TestCase. The tests are public methods that expect no parameters and are named test* Inside the test methods, assertion methods such as assertEquals() are used to assert that an actual value matches an expected value.

The Command-Line Test Runner The PHPUnit command-line test runner can be invoked through the phpunit command. >phpunit ArrayTest PHPUnit by Sebastian Bergmann... Time: 00:00 OK (2 tests).  Printed when the test succeeds. F  Printed when an assertion fails while running the test method. E  Printed when an error occurs while running the test method. S  Printed when the test has been skipped. I  Printed when the test is marked as being incomplete or not yet implemented.

Incomplete Tests public function testSomething() { // Stop here and mark this test as incomplete. $this->markTestIncomplete( 'This test has not been implemented yet.‘); } A test as being marked as incomplete or not yet implemented.

Skipping Tests markTestSkipped( 'The MySQLi extension is not available.' ); } } } ?>

Fixtures setUp() method – is called before a test method run tearDown() method – is called after a test method run setUp() and tearDown() will be called once for each test method run.

Use of setUp() method fixture = array(); // Create the Array fixture. } public function testNewArrayIsEmpty() { // Assert that the size of the Array fixture is 0. $this->assertEquals(0, sizeof($this->fixture)); } public function testArrayContainsAnElement() { $this->fixture[] = 'Element'; // Add an element to the Array fixture. // Assert that the size of the Array fixture is 1. $this->assertEquals(1, sizeof($this->fixture)); } } ?>

TestCase Extensions setExpectedException('Exception'); } } ?> Testing Exceptions

TestCase Extensions expectOutputString('foo'); print 'foo'; } public function testExpectFooActualBar() { $this->expectOutputString('foo'); print 'bar'; } } ?> Testing Output

TestCase Extensions setMaxRunningTime(2); sleep(1); } } ?> Testing Performance

Code-Coverage Analysis How do you find code that is not yet tested? How do you measure testing completeness? >phpunit --report./report SomeTest PHPUnit by Sebastian Bergmann..... Time: 00:00 OK (4 tests) Generating report, this may take a moment.

Lines of code that were executed while running the tests are highlighted green, lines of code that are executable but were not executed are highlighted red, and "dead code" is highlighted orange.

Advantages Testing gives code authors and reviewers confidence that patches produce the correct results. Detect errors just after code is written The tests are run at the touch of a button and present their results in a clear format. Tests run fast The tests do not affect each other. If some changes are made in one test, the results of others tests do not change.

Disadvantages Some people have trouble with getting started: where to put the files, how big the scope of one unit test is and when to write a separate testing suite and so on. It would be difficult to write a test for people who are not programmers or familiar with PHP.

Questions?