0
An Introduction to Unit Test       Using NUnit           Wei Li           08/2007
Test, Test and More Test•   Developer Test      o   Unit Test      o   Integration Test      o   Regression Test•   QA Tes...
What Is Unit Test• Self checking or validation to ensure your  code works as expected• Developer’s test• Part of the const...
Where Does Unit Test           Sit In Life Cycle• Requirement Gathering• Business System Design• Technical Design   o Arch...
Unit Test Is Not• QA test• Customer/user test
Why Unit Test• What? Write code to test code?• Why?
Instant Feedback• Unit test provides the earliest opportunity to  identify and fix a defect• The earlier a defect is caugh...
Help Design And Write Better             Code• If your code is hard to be unit tested, how  can it be easily used, maintai...
Regression Checker• When new release of the external dependency  comes, you can quickly run the unit tests to make  certai...
Good Way To Work On Legacy           Code• Write unit test to get started with legacy  code
Good Way To Learn New      Language and New API• Write unit test cases to learn and try new  API• Your unit test cases bec...
Good For Reporting a Defect• Write a test case to report a defect or bug in  a system• When something fails, writing a uni...
Save Time In Long Term• Unit test is reusable and repeatable• Unit test can be automated• Write once run forever
There Is More Than One Way To             Do It• Main() method with lots of print out• GUI “push that button” test• Debugg...
Problems With The Above              Methods•   Is the test well structured?•   Can it be automated?•   Is the test repeat...
Stop Debugger Test• Debugger time is not recyclable• Debugger session is not reusable by others• Not a regression testing ...
Enter NUnit• A unit test framework written in C#• It uses attributes to identify unit test fixtures• It uses assertions fo...
How Does NUnit Test Code Look            Like?• Show me the code
NUnit Attributes• Custom attribute injects more information  to your class at compilation time• NUnit uses it to mark and ...
NUnit Required Attributes– [TestFixture]   • Used to indicate that a class contains test methods– [Test]   • Used to indic...
NUnit Optional Attributes– [SetUp]   • Used to indicate a setup method should be ran before each of     the tests– [TearDo...
NUnit Optional Attributes– [TestFixtureSetUp]    • Used to indicate a setup method that will be ran once; before      all ...
NUnit Optional Attributes– [ExpectedException(typeof(Exception))]    • When you want an exception to be thrown    • Will o...
NUnit Assertion• It is all unit test cares about• NUnit provides a full set of assertions ready  to be used• Assertion fai...
NUnit Assertion•   Equality Asserts•   Identity Asserts•   Comparison Asserts•   Type Asserts•   Condition tests•   Utilit...
NUnit Equal Assertion• Assert.AreEqual(expected, real_value)• Assert.AreEqual(expected, real_value,  “a message”)• Assert....
NUnit Identity Assertion• Assert.AreSame(expected, real_value)• Assert.AreSame (expected, real_value,  “a message”)• Asser...
NUnit Condition Assertion•   Assert.IsTrue()•   Assert.IsFalse()•   Assert.IsNull()•   Assert.IsNotNull()•   Assert.IsEmpt...
ExamplesShow me the code
Test Driven/Test First               Development•   Write a unit test case for a new functionality•   Run the unit test an...
Continuous Integration• Get the latest version of the project from source  control system• Build/compile the code• Run all...
Continuous Integration• Demo: using NAnt to build solution, run  test cases, generate test result reports
Questions?
Happy Testing!
Upcoming SlideShare
Loading in...5
×

An Introduction to Unit Test Using NUnit

3,790

Published on

An introduction to unit test using NUnit

Published in: Technology
2 Comments
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total Views
3,790
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
167
Comments
2
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "An Introduction to Unit Test Using NUnit"

  1. 1. An Introduction to Unit Test Using NUnit Wei Li 08/2007
  2. 2. Test, Test and More Test• Developer Test o Unit Test o Integration Test o Regression Test• QA Test o Integration Test o Regression Test o Performance Test o Stress Test• Customer/User Test o Acceptability Test o Useability Test
  3. 3. What Is Unit Test• Self checking or validation to ensure your code works as expected• Developer’s test• Part of the construction cycle
  4. 4. Where Does Unit Test Sit In Life Cycle• Requirement Gathering• Business System Design• Technical Design o Architecture Design o Technical System Design• Construction o Code and Unit Test• QA Test• User Test
  5. 5. Unit Test Is Not• QA test• Customer/user test
  6. 6. Why Unit Test• What? Write code to test code?• Why?
  7. 7. Instant Feedback• Unit test provides the earliest opportunity to identify and fix a defect• The earlier a defect is caught, the less expensive it is to fix it• Done before integration• Like a dedicated QA/user sits next to you and test your code immediately
  8. 8. Help Design And Write Better Code• If your code is hard to be unit tested, how can it be easily used, maintained and extended?
  9. 9. Regression Checker• When new release of the external dependency comes, you can quickly run the unit tests to make certain the new release won’t break your system• Automatic change detector – When changes are made to the code, automated unit tests ensure that the change does not break something somewhere
  10. 10. Good Way To Work On Legacy Code• Write unit test to get started with legacy code
  11. 11. Good Way To Learn New Language and New API• Write unit test cases to learn and try new API• Your unit test cases become reusable knowledge base
  12. 12. Good For Reporting a Defect• Write a test case to report a defect or bug in a system• When something fails, writing a unit test for that failure guarantees that its fixed for once and for all.
  13. 13. Save Time In Long Term• Unit test is reusable and repeatable• Unit test can be automated• Write once run forever
  14. 14. There Is More Than One Way To Do It• Main() method with lots of print out• GUI “push that button” test• Debugger test
  15. 15. Problems With The Above Methods• Is the test well structured?• Can it be automated?• Is the test repeatable?• Does it need human interaction?• Is it easy to be maintained ?• How to present test results?
  16. 16. Stop Debugger Test• Debugger time is not recyclable• Debugger session is not reusable by others• Not a regression testing tool• Mental assertion is error-prone and boring
  17. 17. Enter NUnit• A unit test framework written in C#• It uses attributes to identify unit test fixtures• It uses assertions for verification• It can be used to test any .NET code, not just C#• It provides a console and GUI interface• It can be integrated into VS.NET
  18. 18. How Does NUnit Test Code Look Like?• Show me the code
  19. 19. NUnit Attributes• Custom attribute injects more information to your class at compilation time• NUnit uses it to mark and identify unit test fixture
  20. 20. NUnit Required Attributes– [TestFixture] • Used to indicate that a class contains test methods– [Test] • Used to indicate that a method within a test fixture should be run by the Test Runner applicationExample: Test1.cs
  21. 21. NUnit Optional Attributes– [SetUp] • Used to indicate a setup method should be ran before each of the tests– [TearDown] • Used to indicate a tear down method should be ran after each of the tests are ranExample: Test2.cs
  22. 22. NUnit Optional Attributes– [TestFixtureSetUp] • Used to indicate a setup method that will be ran once; before all other tests. This is the first method that is called before the tests are started.– [TestFixtureTearDown] • Used to indicate a tear down method that will be ran once; after all other tests have run. This is the last method that is called after all the tests have finished.Example: Test3.cs
  23. 23. NUnit Optional Attributes– [ExpectedException(typeof(Exception))] • When you want an exception to be thrown • Will only pass if exception type was thrown– [Ignore(“Not ready yet")]Example: Test4.cs
  24. 24. NUnit Assertion• It is all unit test cares about• NUnit provides a full set of assertions ready to be used• Assertion failure means test failure
  25. 25. NUnit Assertion• Equality Asserts• Identity Asserts• Comparison Asserts• Type Asserts• Condition tests• Utility methods
  26. 26. NUnit Equal Assertion• Assert.AreEqual(expected, real_value)• Assert.AreEqual(expected, real_value, “a message”)• Assert.AreNotEqual(expected, real_value)• Assert.AreNotEqual(expected, real_value, “a message”)
  27. 27. NUnit Identity Assertion• Assert.AreSame(expected, real_value)• Assert.AreSame (expected, real_value, “a message”)• Assert.AreNotSame (expected, real_value)• Assert.AreNotSame (expected, real_value, “a message”)
  28. 28. NUnit Condition Assertion• Assert.IsTrue()• Assert.IsFalse()• Assert.IsNull()• Assert.IsNotNull()• Assert.IsEmpty()• Assert.IsNotEmpty()
  29. 29. ExamplesShow me the code
  30. 30. Test Driven/Test First Development• Write a unit test case for a new functionality• Run the unit test and it will fail• Write just enough code to make the test pass• Run the unit test again and it passes• Refactory the code to make it better• Repeat the unit test• Repeat the cycle
  31. 31. Continuous Integration• Get the latest version of the project from source control system• Build/compile the code• Run all unit test cases• Publish the build and test results• Notify the development team for any failure• Repeat the process periodically, for example, once every hour
  32. 32. Continuous Integration• Demo: using NAnt to build solution, run test cases, generate test result reports
  33. 33. Questions?
  34. 34. Happy Testing!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×