On October 23rd, 2014, we updated our
By continuing to use LinkedIn’s SlideShare service, you agree to the revised terms, so please take a few minutes to review them.
An Introduction to Unit Test Using NUnitPresentation Transcript
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 Test o Integration Test o Regression Test o Performance Test o Stress Test• Customer/User Test o Acceptability Test o Useability Test
What Is Unit Test• Self checking or validation to ensure your code works as expected• Developer’s test• Part of the construction cycle
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
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 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
Help Design And Write Better Code• If your code is hard to be unit tested, how can it be easily used, maintained and extended?
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
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 become reusable knowledge base
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.
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• Debugger test
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?
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
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
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 identify unit test fixture
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
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
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
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
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
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
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
Continuous Integration• Demo: using NAnt to build solution, run test cases, generate test result reports