360 likes | 374 Views
BIT 142:Programming & Data Structures in C#. What is Unit Testing?. Unit Testing (Definition).
E N D
Unit Testing (Definition) • “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.”www.discovercomputers.info/ComputerSecurity/glossary.html
Example Function To Test • 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) • “The number 1 is by definition not a prime number”
Example Unit Tests • 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
What is NUnit? • A free, open-source framework to help people create their own unit tests.
NUnit’s Goal(s): • Make it quick and easyto create unit tests, and to run those tests,and to get a list of which tests passed/failed.
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 that are provided to you, by the teacher
How do I Use NUnit? • 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
Demo: • Quick walkthrough of how to use this • (GUI , Normal console, grade gen)
Demo: • Things you’ll need / find useful: • To select which project will be run: • Right-click on a PROJECT, then select "Set Startup Project" • EditFind And ReplaceFind In Files • This will let you search ALL files for a particular string • How to get VS to comment/uncomment a block
Workflow • 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 • DELETE DIRECTORIES ON NEXT SLIDE!!! • Hand in PCEs
Submitting Your Work • Delete the directory named DELETE_THIS_and_bin_and_obj_folders • DELETE bin AND obj directories!!!
Workflow • Instructor workflow: • Run tests to get basic grade • Double-check code • Grade for feedback/ stuff that’s not auto-graded • Finalize grade & email gradesheets
Select “01_PCE_Test_Runner” as startup project Make sure that in RunTests.cs, the lineintdoThis = RUN_TEST_IN_GUI; is uncommented Run in VS, then click the ‘Run’ button in Nunit NUnit should auto-reload the code when you recompile Details: GUI Test Runner BIT 142: Intermediate Programming 16
Be careful about which test(s) you’ve selected Note the ‘Text Output' tab Especially for the BubbleSort exercise Since the BubbleSort uses randomly generated arrays, you may not get the same array twice Details: GUI Test Runner BIT 142: Intermediate Programming 17
Details: Normal Console Program • Select “03_PCE_StudentCode” as startup project & run like normal • The 'split file' thing: • Student_Answers.cs contains the code you will write, INCLUDING main!!
Details: Normal Console Program • 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#
Select “PCE_Test_Runner” as startup project Make sure that in RunTests.cs, the lineint doThis = RUN_TESTS_UNDER_DEBUGGER; is uncommented Put a breakpoint in the test (or your code) Run in VS you MUST choose DebugStart Debugging Useful features: Step, Step Into, Step Out Watch windows Details: Using The Debugger BIT 142: Intermediate Programming 20
Select “PCE_Test_Runner” as startup project Make sure that in RunTests.cs, the lineintdoThis = PRODUCE_GRADESHEET; is uncommented Run in VS Everything should run, and the gradesheet should pop up in a web browser Details: Generating A Gradesheet BIT 142: Intermediate Programming 21
Note about grading output: 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 Details: Generating A Gradesheet BIT 142: Intermediate Programming 22
Note that compromising the system in any way will get you a zero Hacking/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?” Details: Generating A Gradesheet BIT 142: Intermediate Programming 23
Note about grading output: 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 Details: Generating A Gradesheet BIT 142: Intermediate Programming 24
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, briefly, in order to make sure you’ve gotten a quick overview of 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 Details: NUnit tests BIT 142: Intermediate Programming 25
Understanding The NUnit Tests • Attributes: • [Test] • Specifies that test will be automatically called by NUnit. • How the name shows up in the GUI
Understanding The NUnit Tests • Attributes to ignore: • [Category] – ignore this • [TestFixture], [TimeOut], [Description] – ignore these • Point out that the class to be tested MUST be public!!
Understanding The NUnit Tests • Execution of a test: • Just like normal, it will be top to bottom • If the function crashes (or throws exception), then the crash will be caught, prevented (normally), and the test will fail • We can also tell NUnit to check that certain things are true at certain points, and fail if they’re not true
Understanding The NUnit Tests • Assert.That( false, “message” ); • What this does
Understanding The NUnit Tests • bool correctAnswer = false; • Assert.That(correctAnswer, “message” );
Understanding The NUnit Tests • int num = 10; • Assert.That( num == 12, “message”);
Understanding The NUnit Tests • “Message” can work like Console.WriteLine: • int num = 10; • Assert.That( num == 12, “num={0} isn’t what we expected”, num);
Understanding The NUnit Tests • More Attributes: • [Values()] – on a parameter
Understanding The NUnit Tests • More Attributes: • [TestCase]
Understanding The NUnit Tests • Console stuff: • Stealing Console.In • Capturing Console.Out • Importance of the ‘fuzzy’ string comparison
Exercises: • Drop existing, working code into a unit test, and watch the tests work • Look at a couple of simple tests • Go through & fiddle with test code, in order to learn it