What is Unit Testing 1 Unit Testing Definition
- Slides: 45
What is Unit Testing? 1
Unit Testing (Definition) Writing snippets of code that try to use methods (functions) from your program. Each snippet should test one (and only one) function. . . by calling the function, then comparing what the function actually produced to what it was supposed to produce. Running the test code thus verifies that (at least part of) your program is doing what it’s supposed to. 2
Example Function To Test public bool is. Prime( 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) “The number 1 is by definition not a prime number” 3
Example Test Cases public bool is. Prime( 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
Using the NUnit-Based Autograded Projects 5
What is NUnit? 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
NUnit’s Goal(s): 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
How do I Use NUnit? 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
How do I Use NUnit? You will download a starter project, which includes all the NUnit materials Extract the. ZIP 9
Details: Solution Setup Open the Solution (. SLN) file ◦ Note multiple projects 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_For. Tests This is where the units tests themselves are located ◦ 03_PCE_Student. Code This is where YOUR CODE must be put 10 10
01_PCE_Test_Runner Ignore anything except the Run. Tests. cs ◦ You can set the ‘mode’ here (you’ll see this later) 11
02_PCE_For. Tests This project contains all the tests Test. Helpers. cs contains stuff that multiple tests, across multiple assignments, make use of ◦ Input/output capturing code ◦ ‘Fuzzy’ comparisons 12
02_PCE_For. Tests PCE_Tests_To_Run. cs contains the actual tests – you’ll want to look in here for the details of each particular test. 13
03_PCE_Student_Code This is where YOUR CODE will be put. ◦ There will be just one file (Student_Answers. cs) ◦ Your code (that you will hand in) always goes into Student_Answers. cs 14
03_PCE_Student_Code 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. 15
Demo: How to run the tests in the NUnit GUI 16
Details: GUI Test Runner Make sure that “ 01_PCE_Test_Runner” is startup project ◦ Right-click on it, and “Set as startup project” if it’s not ◦ Make sure that in Run. Tests. cs, the line do. This = RUN_TEST_IN_GUI; is uncommented In VS: Debug Start (Without) Debugging ◦ (If either doesn’t work, email prof!) In the NUnit test runner: click ‘Run’ button ◦ Be careful about which test(s) you’ve selected 17
Details: GUI Test Runner 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 18
How to find a failing test Run the test(s) in the GUI Get the name of a test that is failing 1. Edit Find And Replace Find In Files a) Type in the name of the failing test & go find it. 2. In 02_PCE_For. Tests, in PCE_Tests_To_Run. cs find the test by hand 19
Details: GUI Test Runner 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 20
Details: The Unit Tests 21
[Test] Attribute [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 22
Attributes to ignore: [Category] ◦ Used by the instructor to put a test into a grading category [Test. Fixture], [Time. Out], [Description] ◦ Used to tell Nunit about tests 23
Execution of a test 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 24
Basic (Failing) Test 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 25
More typical pattern bool correct. Answer = false; Assert. That(correct. Answer, “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 26
Using Assert Like Console. Write* “Message” can work like Console. Write. Line: int num = 10; Assert. That( num == 12, “num={0} isn’t what we expected”, num); Test: Basic_Console_Write. Line 27
Calling Student Code 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 Student. Code project This will either have been done for you, or else we’ll cover how to create your own classes in C# 28
Calling Student Code This is the first ‘real’ unit test Call into your code Check the answer Pass (or fail) the test Test: Basic_Unit_Test 29
Debugging Strategy: Printing 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 30
Test Attribute Details 31
Redirecting I/O 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 32
Output (captured by the test) Using the Test. Helpers software, the tests can capture output. ◦ This is in the Test. Helpers. 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_Write. Line 33
Fuzzy String Comparisons The Test. Helpers 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_Write. Line_Fuzzy 34
Test-Supplied Input 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 ◦ Enter key (Newline) in C#: n Example of input: ◦ “ 2n 3nquitn” is the same as typing 2<enter>3<enter>quit<enter> Test: Unit_Test_Console_IO_Fuzzy 35
[Values] Attribute public void Unit_Test_Values( [Values(-1, 0, 1, 10)]int value. To. Try) Tells NUnit to call this function four separate times: ◦ ◦ once with value. To. Try set set to to -1 0, 1 10 Test: Unit_Test_Values 36
[Values] Attribute - Combo 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 a = = 1, b = 2 1, b = 4 10, b = 2 10, b = 4 Test: Unit_Test_Values_2 37
[Test. Case] Attribute [Test. Case( 1, 2)] [Test. Case( 10, 4)] public void Unit_Test. Case(int a, int b) Tells NUnit to call this function with EACH Test. Case: ◦ a = 1, b = 2 ◦ a = 10, b = 4 Test: Unit_Test. Case 38
Tests that use the [Setup] Attribute 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 ex 5 to refer to an object Test: Unit_Test_Setup 39
Demo: How to Generate a gradesheet (including grade) 40
Details: Autograder Not all tests that you can see in the GUI are neccessarily graded Failed tests are big, but passed tests are kinda small & on the bottom 41
Details: Autograder 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 ‘Find. In. Array’ 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? ” 42
Demo: How to run the program in normal console mode 43
Normal Console Program Make sure that 03_PCE_Student. Code is the startup project ◦ Right-click on the PROJECT, and choose “Set as Startup Project” to make it the startup project Put whatever code you want to into the main method. 44
Normal Console Program If you want, you can always create a new console solution, then copy your code into it. 45
- Functional testing vs unit testing
- Domain closure in software testing
- Motivational overview of logic based testing
- Du path testing
- Positive vs negative testing
- Static testing and dynamic testing
- What is globalization testing
- Cause effect graph for triangle problem
- Control structure testing in software testing
- Decision table testing in software testing
- Decision table testing
- Apa yang dimaksud dengan black box testing
- Behavior testing adalah
- Decision tables testing
- Rigorous testing in software testing
- Testing blindness in software testing
- Component testing is a black box testing
- Software testing domains
- Unit 10, unit 10 review tests, unit 10 general test
- Is unit testing verification or validation
- Flex unit testing
- What is unit testing in software
- Ruby unit testing
- Rails test database
- Ssisunit
- Pl/sql unit testing
- Manfaat dari unit testing
- Plc unit testing
- Hungarian notation vba
- Postgres unit testing
- Unit test design patterns
- Biztalk unit testing
- Unit 5 application of fitness testing
- Dynamic unit testing
- Aaa unit testing
- Unit xi testing and individual differences
- Cute: a concolic unit testing engine for c
- Embedded software unit testing
- Definition of software testing
- Conclusion of non destructive testing
- Hypothesis testing meaning
- Scanner market testing
- Agile testing definition
- Hypothesis testing definition
- Durability example
- Definition of software testing