Unit Testing Using PyUnit Monther Suboh 200820311 Yazan Hamam 200810019 Saddam Al-Mahasneh 200810845 Miran Ahmad 200810194.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Testing/Testing In Rails 1 Alan and Saskia 2/8/2008.
Testing Object Oriented Programs CSE 111 4/28/20151.
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
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.
FIT FIT1002 Computer Programming Unit 19 Testing and Debugging.
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
Composition One object is composed of other objects (e.g. Car is composed of an engine, etc.) Engine cylinders : int volume: int start() : None Car engine.
Approach of Unit testing with the help of JUnit Satish Mishra
© S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
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.
© S. Demeyer, S. Ducasse, O. Nierstrasz Chapter.1 Unit Testing Explained How to support changes? How to support basic but synchronized documentation?
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
Automated Tests in NICOS Nightly Control System Alexander Undrus Brookhaven National Laboratory, Upton, NY Software testing is a difficult, time-consuming.
Image processing algorithm regression testing framework Soumik Ukil.
Programmer Testing Testing all things Java using JUnit and extensions.
Terms: Test (Case) vs. Test Suite
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
M. Gallas IT-API LCG SPI project: testing1 Software Testing Infrastructure status LCG Software Process & Infrastructure (CERN, 10/23/02)
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
SCRAM Software Configuration, Release And Management Background SCRAM has been developed to enable large, geographically dispersed and autonomous groups.
M Gallas CERN EP-SFT LCG-SPI: SW-Testing1 LCG-SPI: SW-Testing LCG Applications Area GridPP 7 th Collaboration Meeting LCG/SPI LCG.
Object Oriented Programming
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Testing in Extreme Programming
Unit Testing Continuous Integration PYUNIT AND JENKINS FRAMEWORK Presenter Rachita Agasthy.
Continuous QA Sewit Adams (Colorado State University) Bin Gao (Michigan State University) Jerry Neal (Indiana University)
CSE 219 Computer Science III Testing. Testing vs. Debugging Testing: Create and use scenarios which reveal incorrect behaviors –Design of test cases:
Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools.
Chapter 24 Exception CSC1310 Fall Exceptions Exceptions Exceptions are events that can modify the flow or control through a program. They are automatically.
Software Construction Lecture 18 Software Testing.
LCG-SPI: SW-Testing LCG AppArea internal review (20/10/03)
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
A tool for test-driven development
M Gallas CERN EP-SFT LCG-SPI: SW-Testing1 LCG-SPI: SW-Testing QMTest test framework LCG AppArea meeting (16/07/03) LCG/SPI LCG Software.
EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group.
JUnit Don Braffitt Updated: 10-Jun-2011.
Getting Started with SIDL using the ANL SIDL Environment (ASE) ANL SIDL Team MCS Division, ANL April 2003 The ANL SIDL compilers are based on the Scientific.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
Test a Little, Code a Little Colin Sharples IBM Global Services New Zealand Colin Sharples IBM Global Services New Zealand.
PROGRAMMING TESTING B MODULE 2: SOFTWARE SYSTEMS 22 NOVEMBER 2013.
FIT Objectives By the end of this lecture, students should: understand the role of constructors understand how non-default constructors are.
Topic: Junit Presenters: Govindaramanujam, Sama & Jansen, Erwin.
Testing Overview Software Reliability Techniques Testing Concepts CEN 4010 Class 24 – 11/17.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Testing Unit Testing In Evergreen Kevin Beswick Laurentian University / Project Conifer.
Mutation Testing Laraib Zahid & Mariam Arshad. What is Mutation Testing?  Fault-based Testing: directed towards “typical” faults that could occur in.
Automated Testing with PHPUnit. How do you know your code works?
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
1 March 12, Testing William Cohen NCSU CSC 591W March 12, 2008.
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.
Object Oriented Testing (Unit Testing)
CSE 219 Final exam review.
Software Testing.
Don Braffitt Updated: 26-Mar-2013
Testing Verification and the Joy of Breaking Code
Smalltalk Testing - SUnit
Software Testing.
Java Unit 11: Inheritance I
Computer Science 209 Testing With JUnit.
History, Characteristics and Frameworks
Lecture 09:Software Testing
Introduction to Testing, SUnit and Error Handling
CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week.
Designing For Testability
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
Presentation transcript:

Unit Testing Using PyUnit Monther Suboh Yazan Hamam Saddam Al-Mahasneh Miran Ahmad

Overview Unit Testing Basics: What, Why, When How: Python Unit Test Class Unit Test Writing Tips and Resources

Unit Testing Basics Functional tests: done by QA to test functionality according to a test plan based on requirements and design specs. Unit tests: done by developers to test specific code. Typically “white box” testing. Essential part of Extreme Programming and other agile methods.

Why Write Unit Tests Increase developers’ confidence in code. If someone challenges your work, you can say “the tests passed.” Avoid regression. If unit test suite is run frequently, you know when new code breaks old code. If you write tests first, you know when you’re done, i.e., when the tests pass. Encourages minimal interfaces and modularity.

When to Write/Run Unit Tests Always! Before you check code into repository, so you know your code works. Before debugging, to ease the process and help you know when you’re done.

Test Writing Tips Make code modular: use interfaces/template classes/abstract base classes. Use mock objects to inspect behavior of object you’re testing and to stand in for “heavy” objects, e.g., you don’t want to do network I/O in a unit test. Modular, loosely coupled interfaces make mock objects possible. Excessive coupling is enemy of unit testing.

Using PyUnit to write your own tests Installation The classes needed to write tests are to be found in the 'unittest' module. This module is part of the standard Python library for Python 2.1 and later. If you are using an older Python version, you should obtain the module from the separate PyUnit distribution.

An introduction to TestCases The basic building blocks of unit testing are 'test cases' -- single scenarios that must be set up and checked for correctness. In PyUnit, test cases are represented by the TestCase class in the unittest module. To make your own test cases you must write subclasses of TestCase. An instance of a TestCase class is an object that can completely run a single test method, together with optional set-up and tidy-up code.

Creating a simple test case The simplest test case subclass will simply override the runTest method in order to perform specific testing code: import unittest class DefaultWidgetSizeTestCase(unittest.TestCase): def runTest(self): widget = Widget("The widget") assert widget.size() == (50,50), 'incorrect default size'

Note that in order to test something, we just use the built-in 'assert' statement of Python. If the assertion fails when the test case runs, an AssertionError will be raised, and the testing framework will identify the test case as a 'failure'. Other exceptions that do not arise from explicit 'assert' checks are identified by the testing framework as 'errors'.

Re-using set-up code: creating 'fixtures' Now, such test cases can be numerous, and their set-up can be repetitive. In the above case, constructing a 'Widget' in each of 100 Widget test case subclasses would mean unsightly duplication. Luckily, we can factor out such set-up code by implementing a hook method called setUp, which the testing framework will automatically call for us when we run the test:

import unittest class SimpleWidgetTestCase(unittest.TestCase): def setUp(self): self.widget = Widget("The widget") class DefaultWidgetSizeTestCase(SimpleWidgetTestCase): def runTest(self): assert self.widget.size() == (50,50), 'incorrect default size' class WidgetResizeTestCase(SimpleWidgetTestCase): def runTest(self): self.widget.resize(100,150) assert self.widget.size() == (100,150), \ 'wrong size after resize'

If the setUp method raises an exception while the test is running, the framework will consider the test to have suffered an error, and the runTest method will not be executed. Similarly, we can provide a tearDown method that tidies up after the runTest method has been run: import unittest class SimpleWidgetTestCase(unittest.TestCase): def setUp(self): self.widget = Widget("The widget") def tearDown(self): self.widget.dispose() self.widget = None If setUp succeeded, the tearDown method will be run regardless of whether or not runTest succeeded. Such a working environment for the testing code is termed a fixture.

TestCase classes with several test methods Often, many small test cases will use the same fixture. In this case, we would end up subclassing SimpleWidgetTestCase into many small one-method classes such as DefaultWidgetSizeTestCase. This is time-consuming and discouraging import unittest class WidgetTestCase(unittest.TestCase): def setUp(self): self.widget = Widget("The widget") def tearDown(self): self.widget.dispose() self.widget = None def testDefaultSize(self): assert self.widget.size() == (50,50), 'incorrect default size' def testResize(self): self.widget.resize(100,150) assert self.widget.size() == (100,150), \ 'wrong size after resize'

TestCase classes with several test methods Here we have not provided a runTest method, but have instead provided two different test methods. Class instances will now each run one of the test methods, with self.widget created and destroyed separately for each instance. When creating an instance we must specify the test method it is to run. We do this by passing the method name in the constructor: defaultSizeTestCase = WidgetTestCase("testDefaultSize") resizeTestCase = WidgetTestCase("testResize")

Running tests The unittest module contains a function called main, which can be used to easily turn a test module into a script that will run the tests it contains. The main function uses the unittest.TestLoader class to automatically find and load test cases within the current module. Therefore, if you name your test methods using the test* convention, you can place the following code at the bottom of your test module: if __name__ == "__main__": unittest.main()

Resources