1.  “To write test cases for every non-trivial function or method in the module so that each test case is [as] separate from the others [as] possible.”

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
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.
Programming in Visual Basic
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
JavaScript, Fourth Edition
DEBUGGERS For CS302 Data Structures Course Slides prepared by TALHA OZ (most of the text is from
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
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.
Moodle (Course Management Systems). Assignments 1 Assignments are a refreshingly simple method for collecting student work. They are a simple and flexible.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
BIT 142:Programming & Data Structures in C#. How To Use NUnit-based Starter Projects.
Introduction to Testing 1. Testing  testing code is a vital part of the development process  the goal of testing is to find defects in your code  Program.
Debugging in Java. Common Bugs Compilation or syntactical errors are the first that you will encounter and the easiest to debug They are usually the result.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
BIT 142:Programming & Data Structures in C#. What is Unit Testing? 2.
Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In.
Introduction to Exception Handling and Defensive Programming.
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.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Presented by IBM developer Works ibm.com/developerworks/ 2006 January – April © 2006 IBM Corporation. Making the most of The Eclipse debugger.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
1.NET Web Forms Visual Studio © 2002 by Jerry Post.
File Input and Output (I/O) Engineering 1D04, Teaching Session 7.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
1 CS161 Introduction to Computer Science Topic #9.
1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program.
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 4 Slide 1 Slide 1 What we'll cover here l Using the debugger: Starting the debugger Setting.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
Exceptions and Assertions Chapter 15 – CSCI 1302.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
Functions.  Assignment #2 is now due on Wednesday, Nov 25 th  (No Quiz)  Go over the midterm  Backtrack and re-cover the question about tracing the.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
PHP Form Processing * referenced from
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Today protected access modifier Using the debugger in Eclipse JUnit testing TDD Winter 2016CMPE212 - Prof. McLeod1.
Introduction to Exceptions in Java CS201, SW Development Methods.
CSC 108H: Introduction to Computer Programming
Appendix A Barb Ericson Georgia Institute of Technology May 2006
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
User-Written Functions
BIT116: Scripting Lecture 06
Computer Programming I
Scripts & Functions Scripts and functions are contained in .m-files
Chapter 4 – Control Structures Part 1
Intro to PHP & Variables
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Conditions and Ifs BIS1523 – Lecture 8.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Topic 1: Problem Solving
Introduction to TouchDevelop
Chapter 3 – Introduction to C# Programming
Due Next Monday Assignment 1 Start early
Python programming exercise
Don’t hack / crack the system!
Workshop for Programming And Systems Management Teachers
Presentation transcript:

1

 “To write test cases for every non-trivial function or method in the module so that each test case is [as] separate from the others [as] possible.” ecurity/glossary.html ecurity/glossary.html 2

 public bool isPrime( int num) { /*implementation omitted*/ }  Definition of a prime number: ◦ “A prime number (or a prime) is a natural number which has exactly two distinct natural number divisors: 1 and itself.” (Wikipedia)natural number divisors1  “The number 1 is by definition not a prime number”by definition not a prime number 3

 public bool isPrime( int num) { /*implementation omitted*/ }  Test with: ◦ small primes (2, 3, 5, 7, 11) ◦ large primes ◦ non-primes (1, 4, 9, 12, 11*17) ◦ zero ◦ negative numbers 4

5

 A free, open-source framework to help people create their own unit tests for any.Net language ◦ We’ll use it for C#  Related to (“inspired by”) JUnit ◦ JUnit is for Java testing 6

 Make it quick and easy to create unit tests, and to run those tests, and to get a list of which tests passed/failed. 7

 For this class, you will NOT be required to create your own Unit Tests.  You ARE required to write software that passes tests. The tests are provided to you, by the teacher 8

 You will download a starter project, which includes all the NUnit materials  A quick example of how this all will work will follow this slide 9

 Quick walkthrough of how to use this ◦ Run the tests in the NUnit GUI ◦ Generate a gradesheet (including grade) ◦ Run the program in normal console mode 10

11

12

13

 Things you’ll need / find useful: ◦ To select which project will be run:  Right-click on a PROJECT, then select "Set Startup Project" ◦ Edit  Find And Replace  Find In Files  This will let you search ALL files for a particular string ◦ How to get VS to comment/uncomment a block 14

How will I use NUnit as part of my work in this particular course? 15

 Student Workflow: ◦ Download starter project ◦ Examine first exercise, in Word.DOC  Figure out which tests are used in the exercise  Run tests, figure out which ones have failed  Write code to make one (or more) tests pass  Repeat until all tests pass  Repeat until all exercises done ◦ Hand in Student_Answers.cs 16

.ZIP a folder that contains your work, even if it’s only 1 file ◦ Student_Answers.cs ◦ 17

 Instructor workflow: ◦ Run tests to get basic grade ◦ Double-check code ◦ Grade for feedback/ stuff that’s not auto- graded ◦ Finalize grade & gradesheets 18

19

20  There are three projects inside the starter solution: ◦ 01_PCE_Test_Runner  This contains all the (instructor-provided) code that handles running the tests.  This includes running the test in the GUI, and running the auto-grading logic ◦ 02_PCE_ForTests  This is where the units tests themselves are located ◦ 03_PCE_StudentCode  This is where YOUR CODE must be put Details: Solution Setup

 Mostly, you will NOT need to know about anything in here 21

 The only exception is RunTests.cs  You set the ‘mode’ from GUI to auto-grade by adjusting the doThis variable in Main ◦ (For a console application, you’ll need to set 03_PCE_Student_Code as the startup project) 22

 This project contains all the tests  TestHelpers.cs contains stuff that multiple tests, across multiple assignments, make use of ◦ Input/output capturing code ◦ ‘Fuzzy’ comparisons 23

 PCE_Tests_To_Run.cs contains the actual tests – you’ll want to look in here for the details of a particular test. 24

 This is where YOUR CODE will be put. ◦ There may be just one file (Student_Answers.cs), or two (Student_Answers.cs and Program.cs) ◦ Your code (that you will hand in) always goes into Student_Answers.cs 25

 If you want to experiment with a normal, console application, you do so by setting this project as the startup project, and then adding code to Main. 26

27

28

 Select “01_PCE_Test_Runner” as startup project ◦ Make sure that in RunTests.cs, the line doThis = RUN_TEST_IN_GUI; is uncommented  In VS: Debug  Start Without Debugging  In the NUnit test runner: click ‘Run’ button ◦ Be careful about which test(s) you’ve selected 29

 NUnit should auto-reload your code when you recompile ◦ If it doesn’t, you can force it to reload: File  Reload Project or File  Reload Tests  You can then leave the test runner open, switch back to Visual Studio, change your program, and re-run your tests ◦ Without having to quit-and-restart NUnit 30

 Run the test(s) in the GUI  Get the name of a test that is failing 1. Edit  Find And Replace  Find In Files 2. In 02_PCE_ForTests, in PCE_Tests_To_Run.cs find the test by hand 31

 Note the tabs at the bottom:  Errors And Failures: ◦ Lists out the exact error message when the test failed ◦ Tries to tell you where the failure occurred  Tests Not Run: ◦ This should never happen (unless there’s an error with the test)  Test Output: ◦ Anything you (or the test) Console.Writes will be displayed here 32

33

 Select “01_PCE_Test_Runner” as startup project ◦ Make sure that in RunTests.cs, the line doThis = PRODUCE_GRADESHEET; is uncommented  In VS: Debug  Start Without Debugging  You’ll see a lot of text in a console window, and then the gradesheet should appear in a web browser 34

 Not all tests that you can see in the GUI are graded  Failed tests are big, but passed tests are kinda small & on the bottom 35

 Note that compromising the system in any way will get you a zero ◦ Cracking/changing/disabling tests ◦ WRITING CODE TO PASS A TEST, DESPITE NOT ACCOMPLISHING IT’S REAL GOAL  Ex: A ‘FindInArray’ method that just asks “Did I get asked to find the value 8? If so, return true”, so that it passes the test which asks “Is 8 in the array?” 36

37

 Select “01_PCE_Test_Runner” as startup project ◦ Make sure that in RunTests.cs, the line doThis = RUN_TESTS_UNDER_DEBUGGER; is uncommented  In VS: Debug  Start Debugging ◦ AFTER you’ve set some breakpoints 38

 Put a breakpoint in the test (or your code) ◦ Put the cursor on the line, then Debug  Toggle Breakpoint  Useful features:  Step, Step Into, Step Out  Watch windows 39

40

 Located in the 02_PCE_ForTests project, inside the PCE_Test.cs file  You should never need to change or modify these ◦ We’ll go through them now, in order to make sure you’ve understand how the NUnit tests work. ◦ If you need to disable a test, you can just comment the whole method out, and the rest of the system *should* just work 41

 [Test] ◦ Specifies that test will be automatically called by NUnit. ◦ The GUI automatically finds this test, and gets the name to display in the GUI 42

 [Category] ◦ Used by the instructor to put a test into a grading category  [TestFixture], [TimeOut], [Description] ◦ Used to tell Nunit about tests 43

 NUnit ‘magically’ finds the tests to run ◦ Once the test starts, it runs just like normal code  We can tell NUnit to check that certain things are true at certain points, and fail if they’re not true ◦ This is done using the Assert.That command ◦ If the function crashes (or throws exception), then the crash will be caught, prevented (normally), and the test will fail 44

 Assert.That( false, “msg” );  What this does: ◦ If the first parameter (‘false’) is false, then the test will fail, with the message “msg”  Test: Basic_Fail in the Nunit_Demo project 45

bool correctAnswer = false; Assert.That(correctAnswer, “msg” );  This separates out the ‘is it correct?’ logic from the Assert statement  Notice that we can (and often will) use this pattern ◦ Test: Basic_Fail_2, Basic_Fail_3 46

 “Message” can work like Console.WriteLine:  int num = 10;  Assert.That( num == 12, “num={0} isn’t what we expected”, num);  Test: Basic_Console_WriteLine 47

 Typically, a test will call into your code, check the result, and then Assert that your answer is the same as the expected answer  Because the test code is in another project, you MUST make your classes PUBLIC in the StudentCode project  This will either have been done for you, or else we’ll cover how to create your own classes in C# 48

 This is the first ‘real’ unit test Call into your code Check the answer Pass (or fail) the test  Test: Basic_Unit_Test 49

 Certain tests may redirect input, output ◦ This means that they can ‘feed’ input to your code, and ‘capture’ the output that you produce  Capturing Console.Out in the Test ◦ Importance of the ‘fuzzy’ string comparison  Test-Supplied Console.In 50

 Using the TestHelpers software, the tests can capture output. ◦ This is in the TestHelpers.cs file ; you’re welcome to look at it, if you’re curious about how it works  Anything that you print (using Console.Write) can be captured  Test: Unit_Test_Console_WriteLine 51

 The TestHelpers software also provides for a number of helpful routines, including ‘fuzzy’ comparisons ◦ ‘Fuzzy’ for strings means case-insensitive, and without (too much) regard for blank spaces  Test: Unit_Test_Console_WriteLine_Fuzzy 52

 The test can also assemble a string, which will be used as the input to your code.  Whenever you call Console.Readline, you will read everything up to the next newline ◦ Newline in C#: \n  Example of input: ◦ “2\n3\nquit\n” is the same as typing 2 3 quit  Test: Unit_Test_Console_IO_Fuzzy 53

 public void Unit_Test_Values( [Values(-1, 0, 1, 10)]int valueToTry)  Tells NUnit to call this function with valueToTry set to -1, then set to 0, then 1, then 10  Test: Unit_Test_Values 54

 public void Unit_Test_Values_2( [Values(1, 10)]int a, [Values(2, 4)]int b)  Tells NUnit to call this function with ALL POSSIBLE COMBINATIONS OF a and b: ◦ a = 1, b = 2 ◦ a = 1, b = 4 ◦ a = 10, b = 2 ◦ a = 10, b = 4  Test: Unit_Test_Values_2 55

[TestCase( 1, 2)] [TestCase( 10, 4)] public void Unit_Test_TestCase(int a, int b)  Tells NUnit to call this function with EACH TestCase: ◦ a = 1, b = 2 ◦ a = 10, b = 4  Test: Unit_Test_TestCase 56

 Notice that before each test, the method marked with [Setup] will be run  In the example test, we’ll use this to initialize x to be 10, y to be 20, and ex5 to refer to an object  Test: Unit_Test_Setup 57

 If you’re having trouble getting a test to pass, you can try adding print statements to your code, so that as the program runs, it tells you about what it’s doing 58