You’ll get better code in less time (If you do it for a while)

Slides:



Advertisements
Similar presentations
What is Unit Testing? How TDD Works? Tsvyatko Konov Telerik Corporation
Advertisements

Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
CIT 590 Unit testing.
A Brief Introduction to Test- Driven Development Shawn M. Jones.
Debugging Introduction to Computing Science and Programming I.
Testing an individual module
Software Testing. “Software and Cathedrals are much the same: First we build them, then we pray!!!” -Sam Redwine, Jr.
ECE122 L17: Method Development and Testing April 5, 2007 ECE 122 Engineering Problem Solving with Java Lecture 17 Method Development and Testing.
1 Functional Testing Motivation Example Basic Methods Timing: 30 minutes.
CIT 590 Unit testing. Agenda Debugging attempt 2 (because I am stubborn) What is unit testing Why? Unit testing framework in Python.
Fundamentals of Python: From First Programs Through Data Structures
TDD,BDD and Unit Testing in Ruby
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Test Driven Development TDD. Testing ”Testing can never demonstrate the absence of errors in software, only their presence” Edsger W. Dijkstra (but it.
1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang.
Fundamentals of Python: First Programs
Testing. Definition From the dictionary- the means by which the presence, quality, or genuineness of anything is determined; a means of trial. For software.
Introduction to Unit Testing Jun-Ru Chang 2012/05/03.
Testing in Extreme Programming
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
CSE 219 Computer Science III Testing. Testing vs. Debugging Testing: Create and use scenarios which reveal incorrect behaviors –Design of test cases:
Dr. Tom WayCSC Testing and Test-Driven Development CSC 4700 Software Engineering Based on Sommerville slides.
COMP 121 Week 1: Testing and Debugging. Testing Program testing can be used to show the presence of bugs, but never to show their absence! ~ Edsger Dijkstra.
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.
A Practical Guide To Unit Testing John E. Boal TestDrivenDeveloper.com.
(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.
1 Legacy Code From Feathers, Ch 2 Steve Chenoweth, RHIT Right – Your basic Legacy, from Subaru, starting at $ 20,295, 24 city, 32 highway.
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.
1 Program Planning and Design Important stages before actual program is written.
Test-Driven Development Eduard Miric ă. The problem.
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,
Software Engineering Lecture # 1.
Intermediate 2 Computing Unit 2 - Software Development.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
How to Test Methods Computer Science 3 Gerb Objective: Test methods properly.
Software Quality Assurance and Testing Fazal Rehman Shamil.
Unit Testing & Code Coverage Please download and install EclEmma (link in resources section…I recommend Option 2) Also snarf the junit code for today’s.
Automated Testing with PHPUnit. How do you know your code works?
1 Sections 7.2 – 7.7 Nested Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CSC 108H: Introduction to Computer Programming
Testing Data Structures
Software Testing.
Test-driven development
CIT 590 Unit testing.
Formatting Output.
Test Driven Development 1 November Agenda  What is TDD ?  Steps to start  Refactoring  TDD terminology  Benefits  JUnit  Mocktio  Continuous.
Testing and Debugging.
Testing the Software with Blinders on
Topics Introduction to Repetition Structures
Data Structures and Algorithms
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
UNIT-4 BLACKBOX AND WHITEBOX TESTING
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Lecture 09:Software Testing
Test-driven development (TDD)
Testing and Test-Driven Development CSC 4700 Software Engineering
Theory of Computation Turing Machines.
A Few Review Questions.
We’re moving on to more recap from other programming languages
CS 1111 Introduction to Programming Fall 2018
Coding Concepts (Basics)
Significance Tests: The Basics
Baisc Of Software Testing
In the Senior Design Center
Tonga Institute of Higher Education IT 141: Information Systems
Go to pollev.com/cse143.
Review of Previous Lesson
UNIT-4 BLACKBOX AND WHITEBOX TESTING
Testing Slides adopted from John Jannotti, Brown University
Presentation transcript:

You’ll get better code in less time (If you do it for a while)

Workshop Download Start guessing range can be setted. Game have only one answer. User can guess a number in between guessing range After a guess game will tell how far is the answer If guess correct will result ‘HIT’ greater or lesser between 1 - 5 will get ‘Very close’ greater or lesser between 6 - 20 will get ‘Close’ greater or lesser between 21 - 40 will get ‘Not close’ greater or lesser 41 will get ‘Forget it’ User have limited change to guess

Test Driven Development & Unit Test By: Krittayot Techasombooranakit & Supavit Kongwudhikunakorn

Unit Testing Sources:- Computer Science and Engineering, University of Washington Electrical Engineering and Computer Science, Massachusetts Institute of Technology The Art of Unit Testing Martin Fowler

Definition A software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. (src: http://searchsoftwarequality.techtarget.com/definition/unit-testing)

Concept For a given class Rorpap, create another class RorpapTest to test it. RorpapTest contains various “test case” methods to run Each method looks for particular results and passes / fails

(src: http://martinfowler.com/bliki/images/unitTest/sketch.png)

Properties of a good unit test should be automated and repeatable should be easy to implement once it’s written, it should remain for future use anyone should be able to run it should run at the push of a button should run quickly

Tips for writing unit test You cannot test every possible input, parameter value, etc. so you must think of a limited set of tests likely to expose bugs Think about boundary and empty cases positive; zero; negative numbers min/max value right at the edge of an array or collection’s size null; an empty array or list; empty string bugs often occur at boundaries off-by-one error forget to handle empty container arithmetic overflow Test behavior in combination maybe add usually works, but fails after you call remove make multiple calls; maybe size fails the second time only

Trustworthy unit tests Test one thing at a time per test method 10 small tests are better than 1 test 10x as large Each test method should have few (likely 1) assert statements If you assert many things, the first that fails stops the test. You won’t know whether a later assertion would have failed Tests should avoid logic. minimize if/else, loops, switch, etc. avoid try/catch If it’s supposed to throw, use expected

Test case “smells” Tests should be self-contained and not care about each other. “Smells” (bad things to avoid) in tests Constrained test order (i.e. Test A must run before Test B) (usually a misguided attempt to test order/flow) Tests call each other (i.e. Test A calls Test B’s method) (calling a shared helper is OK, though) Mutable shared state (i.e. Tests A/B both use a shared object). (if A breaks it, what happens to B?)

Test coverage

Test coverage If you make a certain level of coverage a target, people will try to attain it. Trouble: High coverage numbers are too easy to reach with low quality testing Trouble: Distracting you from testing the things that really matter Low coverage numbers are a sign of trouble High coverage numbers don’t necessarily assure bugs free

How much testing is enough? You rarely get bugs that escape into production You are rarely hesitant to change some code for fear that it will cause production bugs

Unit test frameworks Grunt, Mocha, Chai - Javascript ์๊์๊ืNUnit - .NET JUnit - Java minitest - Ruby py.test - Python SnapTest - PHP

Example of Unit Test

Exercises

Exercise 1 Design a unit test to determine if the input number considered to be a prime number Description: It accepts any input n for n is a number where 1 <= n <= 100 It outputs “prime number” if n is a prime number; otherwise, “not a prime number”; If n is invalid, “invalid”

Exercise 1 - Solution If n is not a number, invalid If n is -1, invalid If n is 0, invalid If n is 1, not a prime number If n is 2, prime number If n is 3, prime number If n is 50, not a prime number If n is 97, prime number If n is 100, not a prime number If n is 101, invalid

Exercise 2 Design a unit test to determine grade by inputting a total scores Description: It accepts any input n for n is a number where 0 <= n <= 100 n is considered valid only when 0 <= n <= 100 n can contains decimal places It outputs corresponding letter grade if n is valid; otherwise, “invalid”

Exercise 2 - Solution If n is not a number, invalid If n is below 0, invalid If n is more than 100, invalid If n is 0, F If n is 50, D If n is 55, D+ If n is 60, C If n is 70, C+ If n is 75, B If n is 80, B+ If n is 85, A If n is 100, A

Test-driven development

Test-driven development Shortest development cycle in software process Quality assure every cycle

TDD Cycle Write test case (Fail) Make some change Refactor (Cleaned) (Pass)

Step-by-step Add test (Design in your head) Run the test (Make sure it’s fail) Write the code Run the test (Make sure it’s pass) Refactor

Behavior-driven development (BDD) Ensure every behavior in the user stories have done Describe in human way (not technician) Good for principal or customer perspective Given-When-Then statements

TDD Write test case (Fail) Make some change (Pass) Refactor (Cleaned)

BDD Fail Pass Cleaned TDD Write behavior test case (Fail)

What you get. Testable code Design and plan before code Documents the design in themselves Actually know what to code Cleaned code Make code significant better

Keep in minds “The 4 Rules of Simple Design” Pass all test If have any fail have to fix immediately DRY - No duplicated code Every piece of behavior must have only one representation Reveal intent Make naming easy to understand Meaningful naming Keep it small Never code what is not use Minimise number of code (no over structure) Hard/External modules may not be worth to test Avoid overtesting Sometime you can’t avoid exploratory coding

Workshop - Guessing games Game must have start and end range of guessing number. Guessing range is between 0 to 100 Start guessing range can be setted Answer randomed at the start of the game in between guessing range. User can guess a number in between guessing range. After a guess game will tell how far is the answer If correct will get ‘HIT’ greater or lesser between 1 - 5 will get ‘Very close’ greater or lesser between 6 - 20 will get ‘Close’ greater or lesser between 21 - 40 will get ‘Not close’ greater or lesser 41 will get ‘Forget it’ User have limited change to guess

Workshop Downloads Clone template Test setup Node.JS Vistual Studio Code (Optional) Clone template open commandline type in “git clone https://github.com/benzsuankularb/js-template.git” cd js-template npm install Test setup mocha

Additional Kind of TDD Bad of TDD Mockist TDD (Outside-In) Classicist TDD (Inside-out) Bad of TDD TDD make your code significant better, Also can make your code significant terrible. Cost much time if not a TDD expert

You’ll get better code in less time