1 / 36

BIT 142:Programming & Data Structures in C#

BIT 142:Programming & Data Structures in C#. What is Unit Testing?. Unit Testing (Definition).

hinesb
Download Presentation

BIT 142:Programming & Data Structures in C#

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BIT 142:Programming & Data Structures in C#

  2. What is Unit Testing?

  3. 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

  4. 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”

  5. 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

  6. Using NUnit

  7. What is NUnit? • A free, open-source framework to help people create their own unit tests.

  8. 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.

  9. 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

  10. 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

  11. Demo: • Quick walkthrough of how to use this • (GUI , Normal console, grade gen)

  12. Demo: • 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

  13. 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

  14. Submitting Your Work • Delete the directory named DELETE_THIS_and_bin_and_obj_folders • DELETE bin AND obj directories!!!

  15. 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

  16. 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

  17. 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

  18. 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!!

  19. 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#

  20. 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 DebugStart Debugging Useful features: Step, Step Into, Step Out Watch windows Details: Using The Debugger BIT 142: Intermediate Programming 20

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. Understanding The NUnit Tests • Attributes: • [Test] • Specifies that test will be automatically called by NUnit. • How the name shows up in the GUI

  27. 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!!

  28. 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

  29. Understanding The NUnit Tests • Assert.That( false, “message” ); • What this does

  30. Understanding The NUnit Tests • bool correctAnswer = false; • Assert.That(correctAnswer, “message” );

  31. Understanding The NUnit Tests • int num = 10; • Assert.That( num == 12, “message”);

  32. 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);

  33. Understanding The NUnit Tests • More Attributes: • [Values()] – on a parameter

  34. Understanding The NUnit Tests • More Attributes: • [TestCase]

  35. Understanding The NUnit Tests • Console stuff: • Stealing Console.In • Capturing Console.Out • Importance of the ‘fuzzy’ string comparison

  36. 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

More Related