Object Oriented Testing (Unit Testing)

Slides:



Advertisements
Similar presentations
JUnit Tutorial Hong Qing Yu Nov JUnit Tutorial The testing problems The framework of JUnit A case study JUnit tool Practices.
Advertisements

Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
J-Unit Framework.
GoogleTest Primer. Outline Basic Concepts Assertions Basic Assertions Binary Comparison String Comparison Floating-Point Comparison Simple Tests Test.
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.
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
Unit Testing Using PyUnit Monther Suboh Yazan Hamam Saddam Al-Mahasneh Miran Ahmad
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Unittest in five minutes Ray Toal How to unit test Not manually, that's for sure You write code that exercises your code Perform assertions.
Python Control of Flow.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
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.
Built-in Data Structures in Python An Introduction.
(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.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
Python Functions.
A tool for test-driven development
Python Let’s get started!.
Function and Function call Functions name programs Functions can be defined: def myFunction( ): function body (indented) Functions can be called: myFunction(
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.
Topic: Junit Presenters: Govindaramanujam, Sama & Jansen, Erwin.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Automated Testing with PHPUnit. How do you know your code works?
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
CSC 108H: Introduction to Computer Programming
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Software Construction Lab 10 Unit Testing with JUnit
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Dept of Computer Science University of Maryland College Park
Lecture 2 Python Basics.
Python Let’s get started!.
Programming For Big Data
Why exception handling in C++?
JavaScript: Functions.
Introduction to Python
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Logical Operators, Boolean Data Types
Selenium HP Web Test Tool Training
Selection CIS 40 – Introduction to Programming in Python
Introduction to Python
Topics Introduction to File Input and Output
Conditions and Ifs BIS1523 – Lecture 8.
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Programming in Pseudocode
CS190/295 Programming in Python for Life Sciences: Lecture 6
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Coding Concepts (Basics)
Introduction to Problem Solving and Control Statements
G. Pullaiah College of Engineering and Technology
Python Syntax Errors and Exceptions
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
COMPUTER PROGRAMMING SKILLS
By Ryan Christen Errors and Exceptions.
Topics Introduction to File Input and Output
CISC101 Reminders Assignment 3 due today.
Introduction to Python
Selamat Datang di “Programming Essentials in Python”
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

Object Oriented Testing (Unit Testing) Damian Gordon

Object Oriented Testing Unit Testing is concerned with testing small chunks of a program, for example, testing a single class or a single method. Python has a library for unit testing called unittest. It provides several tools for creating and running unit tests.

Object Oriented Testing One of the most important classes in unittest is called TestCase which provides a set of methods to compare values, set up tests and clean up after tests are finished. To write unit tests, we create a subclass of TestCase and write individual methods to do the actual testing. Typically we start all of these methods with the name test.

Object Oriented Testing Let’s look at a simple example:

Object Oriented Testing Let’s look at a simple example: import unittest class CheckNumbers(unittest.TestCase): def test_int_float(self): self.assertEqual(1, 1.0) # END test_int_float # END CheckNumbers if __name__ == "__main__": unittest.main() # ENDIF

Object Oriented Testing Let’s look at a simple example: import unittest class CheckNumbers(unittest.TestCase): def test_int_float(self): self.assertEqual(1, 1.0) # END test_int_float # END CheckNumbers if __name__ == "__main__": unittest.main() # ENDIF Create a subclass of the TestCase class

Object Oriented Testing Let’s look at a simple example: import unittest class CheckNumbers(unittest.TestCase): def test_int_float(self): self.assertEqual(1, 1.0) # END test_int_float # END CheckNumbers if __name__ == "__main__": unittest.main() # ENDIF Create a subclass of the TestCase class This test checks if the integer and real value 1 are equal.

Object Oriented Testing Let’s look at a simple example: import unittest class CheckNumbers(unittest.TestCase): def test_int_float(self): self.assertEqual(1, 1.0) # END test_int_float # END CheckNumbers if __name__ == "__main__": unittest.main() # ENDIF Create a subclass of the TestCase class This test checks if the integer and real value 1 are equal. Make sure this is being run as a script

Object Oriented Testing And if we run this, we get:

Object Oriented Testing And if we run this, we get: . ------------------------------------------------- Ran 1 test in 0.020s OK

Object Oriented Testing And if we run this, we get: . ------------------------------------------------- Ran 1 test in 0.020s OK Dot means the test has passed

Object Oriented Testing Let’s try another example:

Object Oriented Testing Let’s try another example: import unittest class CheckNumbers(unittest.TestCase): def test_string_float(self): self.assertEqual(“1”, 1.0) # END test_string_float # END CheckNumbers if __name__ == "__main__": unittest.main() # ENDIF

Object Oriented Testing Let’s try another example: import unittest class CheckNumbers(unittest.TestCase): def test_string_float(self): self.assertEqual(“1”, 1.0) # END test_string_float # END CheckNumbers if __name__ == "__main__": unittest.main() # ENDIF This test checks if the string and real value 1 are equal.

Object Oriented Testing And if we run this, we get:

Object Oriented Testing And if we run this, we get: F ========================================================= FAIL: test_string_float (__main__.CheckNumbers) This test checks if the string and real value 1 are equal. -------------------------------------------------------- Traceback (most recent call last): File "C:/Users/damian/AppData/Local/Programs/Python/Python35-32/CheckNumbers-string-float.py", line 9, in test_string_float self.assertEqual("1", 1.0) AssertionError: '1' != 1.0 Ran 1 test in 0.060s FAILED (failures=1)

Object Oriented Testing And if we run this, we get: F ========================================================= FAIL: test_string_float (__main__.CheckNumbers) This test checks if the string and real value 1 are equal. -------------------------------------------------------- Traceback (most recent call last): File "C:/Users/damian/AppData/Local/Programs/Python/Python35-32/CheckNumbers-string-float.py", line 9, in test_string_float self.assertEqual("1", 1.0) AssertionError: '1' != 1.0 Ran 1 test in 0.060s FAILED (failures=1) “F” means the test has failed

Object Oriented Testing Let’s combine the two tests together:

Object Oriented Testing Let’s combine the two tests together: import unittest class CheckNumbers(unittest.TestCase): def test_int_float(self): self.assertEqual(1, 1.0) # END test_int_float def test_string_float(self): self.assertEqual(“1”, 1.0) # END test_string_float if __name__ == "__main__": unittest.main() # ENDIF

Object Oriented Testing And if we run this, we get:

Object Oriented Testing And if we run this, we get: .F ================================================================= FAIL: test_string_float (__main__.CheckNumbers) ---------------------------=------------------------------------- Traceback (most recent call last): File "C:\Users\damian\AppData\Local\Programs\Python\Python35-32\CheckNumbers.py", line 10, in test_string_float self.assertEqual("1", 1.0) AssertionError: '1' != 1.0 ----------------------------------------------------------------- Ran 2 tests in 0.010s FAILED (failures=1)

Object Oriented Testing And if we run this, we get: .F ================================================================= FAIL: test_string_float (__main__.CheckNumbers) ---------------------------=------------------------------------- Traceback (most recent call last): File "C:\Users\damian\AppData\Local\Programs\Python\Python35-32\CheckNumbers.py", line 10, in test_string_float self.assertEqual("1", 1.0) AssertionError: '1' != 1.0 ----------------------------------------------------------------- Ran 2 tests in 0.010s FAILED (failures=1) “.F” means the first test passed and the second test has failed

Assertion Methods

Object Oriented Testing A test case typically sets certain variables to known values, runs one or more methods or processes, and then show that correct expected results were returned by using TestCase assertion methods. There are a few different assertion methods available to confirm that specific results have been achieved. We already saw assertEqual, which will cause a test failure if the two parameters do not pass an equality check. The inverse, assertNotEqual, will fail if the two parameters do compare as equal.

Object Oriented Testing The assertTrue and assertFalse methods each accept a single expression, and fail if the expression does not pass an IF test. These tests are not checking for the Boolean values True or False, but instead: To pass the assertFalse method the test should return False, None, 0, or an empty list, dictionary, string, set, or tuple. To pass the assertFalse method the test should return True, non-zero numbers, containers with values in.

Methods Description assertEqual assertNotEqual Accept two comparable objects and ensure the named equality holds. assertTrue assertFalse Accept a single expression, and fail if the expression does not pass an IF test. assertGreater assertGreaterEqual assertLess assertLessEqual Accept two comparable objects and ensure the named inequality holds. asssertIn assertNotIn Ensure an element is (or is not) an element in a container object. assertIsNone assertIsNotNone Ensure an element is (or is not) the exact value None (but not another false value). assertSameElements Ensure two container objects have the same elements, ignoring the order. assertSequenceEqualassertDictEqual assertSetEqual assertListEqual assertTupleEqual Ensure two containers have the same elements in the same order. If there's a failure, show a code diff comparing the two lists to see where they differ. The last four methods also test the type of the list. assertRaises Ensure that a specific function call raises a specific exception.

Object Oriented Testing Let’s look at the assertRaises method in a bit more detail. This method can be used to ensure a specific function call raises a specific exception. The test passes if the code inside the with statement raises the proper exception; otherwise, it fails.

Object Oriented Testing Let’s look at an example:

Object Oriented Testing Let’s look at an example: import unittest def MyAverage(seq): return sum(seq) / len(seq) # END average Continued 

Object Oriented Testing  Continued Object Oriented Testing Let’s look at an example: class TestAverage(unittest.TestCase): def test_zero(self): self.assertRaises(ZeroDivisionError, MyAverage, []) # END test_zero def test_with_zero(self): with self.assertRaises(ZeroDivisionError): MyAverage([]) # END test_with_zero # END CLASS TestAverage Continued 

Object Oriented Testing  Continued Object Oriented Testing Let’s look at an example: We can test if a call to MyAverage gives an error is a blank list is passed in class TestAverage(unittest.TestCase): def test_zero(self): self.assertRaises(ZeroDivisionError, MyAverage, []) # END test_zero def test_with_zero(self): with self.assertRaises(ZeroDivisionError): MyAverage([]) # END test_with_zero # END CLASS TestAverage Continued 

Object Oriented Testing  Continued Object Oriented Testing Let’s look at an example: We can test if a call to MyAverage gives an error is a blank list is passed in class TestAverage(unittest.TestCase): def test_zero(self): self.assertRaises(ZeroDivisionError, MyAverage, []) # END test_zero def test_with_zero(self): with self.assertRaises(ZeroDivisionError): MyAverage([]) # END test_with_zero # END CLASS TestAverage The same test, but calling the method directly, which will return a divide-by-zero error, so we need to use the with statement to tidy up. Continued 

Object Oriented Testing  Continued Object Oriented Testing Let’s look at an example: if __name__ == "__main__": unittest.main() # ENDIF

Object Oriented Testing Now let’s look at a more detailed example. Let’s look at a program, and it’s test program: Stats.py Stats-test.py class StatsList class TestValidInputs def mean def median def mode def test_mean def test_median def test_mode

Object Oriented Testing Here’s stats.py: from collections import defaultdict class StatsList(list): def mean(self): return sum(self) / len(self) # END mean Continued 

Object Oriented Testing  Continued Object Oriented Testing Here’s stats.py: def median(self): if len(self) % 2: return self[int(len(self) / 2)] else: idx = int(len(self) / 2) return (self[idx] + self[idx-1]) / 2 # ENDIF # END median Continued 

Object Oriented Testing  Continued Object Oriented Testing Here’s stats.py: def mode(self): freqs = defaultdict(int) for item in self: freqs[item] += 1 mode_freq = max(freqs.values()) modes = [] for item, value in freqs.items(): if value == mode_freq: modes.append(item) # ENDIF # ENDFOR return modes # END mode

Object Oriented Testing We are going to test this program by creating a new file with our testing code in it. So we’ll import unittest, and use the TestCase class from it, to create a setUp method to do initialization for each test. The setUp method accepts no arguments, and allows us to do arbitrary setup before each test is run.

Object Oriented Testing Here’s stats-test.py: from stats import StatsList import unittest class TestValidInputs(unittest.TestCase): def setUp(self): self.stats = StatsList([1,2,2,3,3,4]) # END setUp Continued 

Object Oriented Testing Here’s stats-test.py: Import the program we’ve just created, and it’s main class. from stats import StatsList import unittest class TestValidInputs(unittest.TestCase): def setUp(self): self.stats = StatsList([1,2,2,3,3,4]) # END setUp Continued 

Object Oriented Testing Here’s stats-test.py: Import the program we’ve just created, and it’s main class. from stats import StatsList import unittest class TestValidInputs(unittest.TestCase): def setUp(self): self.stats = StatsList([1,2,2,3,3,4]) # END setUp Import unittest. Continued 

Object Oriented Testing Here’s stats-test.py: Import the program we’ve just created, and it’s main class. from stats import StatsList import unittest class TestValidInputs(unittest.TestCase): def setUp(self): self.stats = StatsList([1,2,2,3,3,4]) # END setUp Import unittest. Create the setup method, to set up values to be tested. Continued 

Object Oriented Testing  Continued Object Oriented Testing Here’s stats-test.py: def test_mean(self): self.assertEqual(self.stats.mean(), 2.5) # END test_mean Continued 

Object Oriented Testing  Continued Object Oriented Testing Here’s stats-test.py: def test_median(self): self.assertEqual(self.stats.median(), 2.5) self.stats.append(4) self.assertEqual(self.stats.median(), 3) # END test_median Continued 

Object Oriented Testing  Continued Object Oriented Testing Here’s stats-test.py: def test_mode(self): self.assertEqual(self.stats.mode(), [2,3]) self.stats.remove(2) self.assertEqual(self.stats.mode(), [3]) # END test_mode

Object Oriented Testing And if we run this, we get:

Object Oriented Testing And if we run this, we get: ... ---------------------------------------------------------- Ran 3 tests in 0.050s OK

Object Oriented Testing And if we run this, we get: ... ---------------------------------------------------------- Ran 3 tests in 0.050s OK “…” means all three tests have passed

Object Oriented Testing The setUp method is never explicitly called inside any of the three test_* methods, the test suite does the call. Also note that test_median alters the list, by adding a “4” to it, yet when test_mode is called, the list has returned to the values specified in setUp. This shows that setUp is called individually before each test, to ensure the test class starts with a clean slate. Tests can be executed in any order, and the results of one test should not depend on any other tests.

Object Oriented Testing TestCase also offers a no-argument tearDown method, which can be used for cleaning up after each and every test on the class has run. This is useful, for example, if we are testing code that does file I/O, our tests may create new files as a side effect of testing; the tearDown method can remove these files and ensure the system is in the same state it was before the tests ran. Test cases should never have side effects.

etc.