SlideShare a Scribd company logo
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 it's 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 application


Example: 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 ran

Example: 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
NUnit Assertion
•   Equality Asserts
•   Identity Asserts
•   Comparison Asserts
•   Type Asserts
•   Condition tests
•   Utility methods
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”)
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”)
NUnit Condition Assertion
•   Assert.IsTrue()
•   Assert.IsFalse()
•   Assert.IsNull()
•   Assert.IsNotNull()
•   Assert.IsEmpty()
•   Assert.IsNotEmpty()
Examples
Show me the code
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
Questions?
Happy Testing!

More Related Content

PPT
N Unit Presentation
PPTX
Unit Testing Using N Unit
PDF
Web automation using selenium.ppt
PPTX
Hybrid automation framework
PDF
Selenium Page Object Model Using Page Factory | Selenium Tutorial For Beginne...
PDF
An Introduction to Test Driven Development
PDF
Page Object Model and Implementation in Selenium
N Unit Presentation
Unit Testing Using N Unit
Web automation using selenium.ppt
Hybrid automation framework
Selenium Page Object Model Using Page Factory | Selenium Tutorial For Beginne...
An Introduction to Test Driven Development
Page Object Model and Implementation in Selenium

What's hot (20)

PPT
Selenium Automation Framework
PDF
Page object pattern
PPTX
TestNG Framework
PPTX
Unit Testing And Mocking
PPSX
PPT
Test Automation Framework Designs
PPTX
Selenium-Locators
PPT
Hybrid Automation Framework Development introduction
PDF
Selenium Maven With Eclipse | Edureka
PDF
Introduction to Robot Framework
PPTX
Unit testing with NUnit
PPT
Selenium
PPT
PPTX
Setting up Page Object Model in Automation Framework
PPT
Hybrid framework
PDF
Spring Security
PPTX
Properties and indexers in C#
PDF
An introduction to unit testing
PDF
Test Driven Development (TDD)
PPT
Test automation using selenium
Selenium Automation Framework
Page object pattern
TestNG Framework
Unit Testing And Mocking
Test Automation Framework Designs
Selenium-Locators
Hybrid Automation Framework Development introduction
Selenium Maven With Eclipse | Edureka
Introduction to Robot Framework
Unit testing with NUnit
Selenium
Setting up Page Object Model in Automation Framework
Hybrid framework
Spring Security
Properties and indexers in C#
An introduction to unit testing
Test Driven Development (TDD)
Test automation using selenium
Ad

Viewers also liked (20)

PPTX
NUnit Features Presentation
PPT
Nunit
PDF
How and what to unit test
PPTX
Unit Testing (C#)
PPTX
Introduction to Unit Testing
PDF
Unit Test Fun
DOCX
PPTX
Comunicaciones katherine ltda
PPT
438lecture1
PPTX
Comunicaciones katherine ltda
PDF
Prej Tregueseve të Teuhid-it
PPT
ми6 исследования в интернете2
PDF
An introduction to the Spring Framework
PPT
12th Kuala Lumpur Boys' Brigade Weekly Meeting
PPT
JetBrains ReSharper
DOCX
Establecimientos comerciales de la manzana 3
PDF
Safe Creative qué hacemos y cómo lo hacemos
PDF
Estrategia de Desarrollo Local Participativo-AGUJAMA
PDF
Sequential file programming patterns and performance with .net
PDF
e-Marke Checkliste für Wohnungen
NUnit Features Presentation
Nunit
How and what to unit test
Unit Testing (C#)
Introduction to Unit Testing
Unit Test Fun
Comunicaciones katherine ltda
438lecture1
Comunicaciones katherine ltda
Prej Tregueseve të Teuhid-it
ми6 исследования в интернете2
An introduction to the Spring Framework
12th Kuala Lumpur Boys' Brigade Weekly Meeting
JetBrains ReSharper
Establecimientos comerciales de la manzana 3
Safe Creative qué hacemos y cómo lo hacemos
Estrategia de Desarrollo Local Participativo-AGUJAMA
Sequential file programming patterns and performance with .net
e-Marke Checkliste für Wohnungen
Ad

Similar to An Introduction to Unit Test Using NUnit (20)

PPTX
Unit testing
PPTX
Unit testing
PPTX
Unit Testing and TDD 2017
DOCX
Test Driven Development
PPTX
Unit tests & TDD
PPTX
Skillwise Unit Testing
PDF
Unit testing - An introduction
PPTX
Unit tests = maintenance hell ?
PPTX
Building unit tests correctly with visual studio 2013
PDF
Unit Testing Guide. Helps to understand the basics of unit testing .
PPTX
Real Life Unit Testing
PPTX
Unit testing
PDF
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
PPT
Automated testing overview
PDF
What Is Unit Testing A Complete Guide With Examples.pdf
PDF
Introduction of unit test to management
ZIP
Unit Testing in Java
PPT
Automated Unit Testing and TDD
PDF
What Is Unit Testing_ A Complete Guide With Examples.pdf
PPS
Unit Testing
Unit testing
Unit testing
Unit Testing and TDD 2017
Test Driven Development
Unit tests & TDD
Skillwise Unit Testing
Unit testing - An introduction
Unit tests = maintenance hell ?
Building unit tests correctly with visual studio 2013
Unit Testing Guide. Helps to understand the basics of unit testing .
Real Life Unit Testing
Unit testing
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Automated testing overview
What Is Unit Testing A Complete Guide With Examples.pdf
Introduction of unit test to management
Unit Testing in Java
Automated Unit Testing and TDD
What Is Unit Testing_ A Complete Guide With Examples.pdf
Unit Testing

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Cloud computing and distributed systems.
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
cuic standard and advanced reporting.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
NewMind AI Monthly Chronicles - July 2025
Diabetes mellitus diagnosis method based random forest with bat algorithm
Cloud computing and distributed systems.
Understanding_Digital_Forensics_Presentation.pptx
Network Security Unit 5.pdf for BCA BBA.
cuic standard and advanced reporting.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The AUB Centre for AI in Media Proposal.docx
MYSQL Presentation for SQL database connectivity
Reach Out and Touch Someone: Haptics and Empathic Computing
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Unlocking AI with Model Context Protocol (MCP)
Advanced methodologies resolving dimensionality complications for autism neur...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Building Integrated photovoltaic BIPV_UPV.pdf

An Introduction to Unit Test Using NUnit

  • 1. An Introduction to Unit Test Using NUnit Wei Li 08/2007
  • 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. What Is Unit Test • Self checking or validation to ensure your code works as expected • Developer’s test • Part of the construction cycle
  • 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. Unit Test Is Not • QA test • Customer/user test
  • 6. Why Unit Test • What? Write code to test code? • Why?
  • 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. 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. 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. Good Way To Work On Legacy Code • Write unit test to get started with legacy code
  • 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. 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 it's fixed for once and for all.
  • 13. Save Time In Long Term • Unit test is reusable and repeatable • Unit test can be automated • Write once run forever
  • 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. 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. 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. 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. How Does NUnit Test Code Look Like? • Show me the code
  • 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. 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 application Example: Test1.cs
  • 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 ran Example: Test2.cs
  • 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. 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. 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. NUnit Assertion • Equality Asserts • Identity Asserts • Comparison Asserts • Type Asserts • Condition tests • Utility methods
  • 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. 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. NUnit Condition Assertion • Assert.IsTrue() • Assert.IsFalse() • Assert.IsNull() • Assert.IsNotNull() • Assert.IsEmpty() • Assert.IsNotEmpty()
  • 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. 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. Continuous Integration • Demo: using NAnt to build solution, run test cases, generate test result reports