CIT 590 Unit testing.

Slides:



Advertisements
Similar presentations
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
Advertisements

CIT 590 Unit testing.
Test-Driven Development “Test first, develop later!” –OCUnit.
CIT 590 Unit testing. Agenda Debugging attempt 2 (because I am stubborn) What is unit testing Why? Unit testing framework in Python.
What is Testing? Testing is the process of finding errors in the system implementation. –The intent of testing is to find problems with the system.
Scalatest. 2 Test-Driven Development (TDD) TDD is a technique in which you write the tests before you write the code you want to test This seems backward,
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
CSC 108H: Introduction to Computer Programming
Numerical Solutions to the Diffusion Equation
Test-driven development
Chapter 4 C Program Control Part I
Python Let’s get started!.
Introduction to Python
Line Continuation, Output Formatting, and Decision Structures
The switch Statement, and Introduction to Looping
Topics Introduction to Repetition Structures
C++ coding standard suggestion… Separate reasoning from action, in every block. Hi, this talk is to suggest a rule (or guideline) to simplify C++ code.
Input Space Partition Testing CS 4501 / 6501 Software Testing
Test Driven Development 1 November Agenda  What is TDD ?  Steps to start  Refactoring  TDD terminology  Benefits  JUnit  Mocktio  Continuous.
Chapter 8 – Software Testing
Software Testing.
The Selection Structure
Algorithm and Ambiguity
Exceptions and files Taken from notes by Dr. Neil Moore
Applied Software Implementation & Testing
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Unit testing C# classes
Teaching slides Chapter 9.
Exceptions 10-Nov-18.
Cracking the Coding Interview
Testing in PowerShell Powered by Pester NATHAN ZIEHNERT 5/25/2018.
Test Driven Development
Line Continuation, Output Formatting, and Decision Structures
Conditions and Ifs BIS1523 – Lecture 8.
Test-driven development (TDD)
Exceptions and files Taken from notes by Dr. Neil Moore
Coding Concepts (Sub- Programs)
10.4 Solving Equations in Factored Form
If selection construct
Solving Quadratic Equations
Coding Concepts (Basics)
Coding Concepts (Standards and Testing)
Computer Science Testing.
If selection construct
Coding Concepts (Data- Types)
Software Testing & Quality Management
Algorithm and Ambiguity
Test Driven Development
Objective Solve quadratic equations by graphing.
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Recursion Taken from notes by Dr. Neil Moore
CISC101 Reminders All assignments are now posted.
Flowcharts and Pseudo Code
ECE 352 Digital System Fundamentals
Exceptions 25-Apr-19.
Test Driven Development
Exceptions 22-Apr-19.
Exceptions 10-May-19.
You’ll get better code in less time (If you do it for a while)
ENERGY 211 / CME 211 Lecture 27 November 21, 2008.
Exceptions 5-Jul-19.
Controlling Program Flow
Starter Look at the hand-out.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Switch Case Structures
Software Testing.
Presentation transcript:

CIT 590 Unit testing

Agenda Finish up the ‘most common first letter of first name’ example What is unit testing Why do it? Unit testing framework in Python

What is unit testing Test individual units of a program. Simplest way to think of it – test every function This does not guarantee that the different functions work together harmoniously. That is the job of integration testing

Why unit test test driven development (TDD) can save you lots of time Tracking a bug down is often the most time consuming part of development When something breaks you want to know exactly which sub sub component has broken down. The Test, Code, Refactor loop In an ideal world, if you implement incorrect code, one of your tests breaks

How many tests? Test out a typical example – some call this the smoke test. You really expect this one to pass. Test out examples at the extreme ends of the input spectrum – empty list, large numbers etc

What can we not test? Input/output functions are hard to test Design your program better for testability Gather all input from user Do all the processing (business logic) with several small functions Print/render the outputs Always separate the logic from the input/output portion.

Hands on example Quadratic equation testing Palindrome We want to do TDD for a program that finds the solutions to the equation ax2 + bx +c = 0 Palindrome

Rules for writing tests The name of a test method must start with the letters 'test', otherwise it will be ignored. This is so that you can write "helper" methods you can call from your tests, but are not directly called by the test framework. Every test method must have exactly one parameter, self. You must put 'self.' in front of every built-in assertion method you call. The tests must be independent of one another, because they may be run in any order. Do not assume they will be executed in the order they occur in the program

Assertions self.assertEqual(expectedResult, actualResult, [message]) Test that the two values are exactly equal. self.assertNotEqual(firstValue secondValue,[message]) Test that the two values are different, and fail if they are equal. self.assertAlmostEqual(expected, actual, [places,[message]]) Test that the two numeric values are equal, after rounding to places decimal places (default is 7). self.assertTrue(booleanCondition,[message]) Test that the booleanCondition is true. self.assertFalse(booleanCondition,[message]) Test that the booleanCondition is false. self.assertRaises(exception, functionName, parameter, ...,) Test that the function functionName, when called with the given (zero or more) parameters, raises the given exception. Note that, for this assertion method, there is no option for a message parameter.

TDD pick a method that doesn't depend on other, untested methods while the method isn't complete: write a test for the desired feature run all tests and make sure the new one fails while any test fails: add/fix just enough code to try to pass the tests refactor the code to make it cleaner

Refactoring

Caution Who tests the test? Be very careful about the assertions you make in a test Anytime you do a refactoring, run the unit tests again