SlideShare ist ein Scribd-Unternehmen logo
1 von 34
Downloaden Sie, um offline zu lesen
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!

Weitere ähnliche Inhalte

Was ist angesagt?

Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBaskar K
 
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...Thomas Weller
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration TestingDavid Berliner
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit TestingJoe Tremblay
 
Unit Test Presentation
Unit Test PresentationUnit Test Presentation
Unit Test PresentationSayedur Rahman
 
TestNG Session presented in PB
TestNG Session presented in PBTestNG Session presented in PB
TestNG Session presented in PBAbhishek Yadav
 
TestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | EdurekaTestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | EdurekaEdureka!
 
TestNG Session presented in Xebia XKE
TestNG Session presented in Xebia XKETestNG Session presented in Xebia XKE
TestNG Session presented in Xebia XKEAbhishek Yadav
 

Was ist angesagt? (20)

Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NET
 
Unit Testing (C#)
Unit Testing (C#)Unit Testing (C#)
Unit Testing (C#)
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration Testing
 
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
UNIT TESTING
UNIT TESTINGUNIT TESTING
UNIT TESTING
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Test ng
Test ngTest ng
Test ng
 
TestNG Framework
TestNG Framework TestNG Framework
TestNG Framework
 
testng
testngtestng
testng
 
Test Complete
Test CompleteTest Complete
Test Complete
 
Unit testing
Unit testingUnit testing
Unit testing
 
Unit Test Presentation
Unit Test PresentationUnit Test Presentation
Unit Test Presentation
 
TestNG Session presented in PB
TestNG Session presented in PBTestNG Session presented in PB
TestNG Session presented in PB
 
Unit testing
Unit testingUnit testing
Unit testing
 
Junit
JunitJunit
Junit
 
TestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | EdurekaTestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | Edureka
 
TestNG Session presented in Xebia XKE
TestNG Session presented in Xebia XKETestNG Session presented in Xebia XKE
TestNG Session presented in Xebia XKE
 

Andere mochten auch

NUnit Features Presentation
NUnit Features PresentationNUnit Features Presentation
NUnit Features PresentationShir Brass
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit testEugenio Lentini
 
Introduction to Unit Testing
Introduction to Unit TestingIntroduction to Unit Testing
Introduction to Unit TestingMike Pearce
 
Comunicaciones katherine ltda
Comunicaciones katherine ltdaComunicaciones katherine ltda
Comunicaciones katherine ltdakatherinareyesq
 
Comunicaciones katherine ltda
Comunicaciones katherine ltdaComunicaciones katherine ltda
Comunicaciones katherine ltdakatherinareyesq
 
Prej Tregueseve të Teuhid-it
Prej Tregueseve të Teuhid-itPrej Tregueseve të Teuhid-it
Prej Tregueseve të Teuhid-itKastriot Mazreku
 
ми6 исследования в интернете2
ми6 исследования в интернете2ми6 исследования в интернете2
ми6 исследования в интернете2Maria Yurkovskaya
 
An introduction to the Spring Framework
An introduction to the Spring FrameworkAn introduction to the Spring Framework
An introduction to the Spring Frameworkweili_at_slideshare
 
12th Kuala Lumpur Boys' Brigade Weekly Meeting
12th Kuala Lumpur Boys' Brigade Weekly Meeting12th Kuala Lumpur Boys' Brigade Weekly Meeting
12th Kuala Lumpur Boys' Brigade Weekly Meeting12thklbb
 
Establecimientos comerciales de la manzana 3
Establecimientos comerciales de la manzana 3Establecimientos comerciales de la manzana 3
Establecimientos comerciales de la manzana 3Cristian Sanchez
 
Safe Creative qué hacemos y cómo lo hacemos
Safe Creative qué hacemos y cómo lo hacemosSafe Creative qué hacemos y cómo lo hacemos
Safe Creative qué hacemos y cómo lo hacemosSafe Creative
 
Estrategia de Desarrollo Local Participativo-AGUJAMA
Estrategia de Desarrollo Local Participativo-AGUJAMAEstrategia de Desarrollo Local Participativo-AGUJAMA
Estrategia de Desarrollo Local Participativo-AGUJAMAAnabella Agujama
 
Sequential file programming patterns and performance with .net
Sequential  file programming patterns and performance with .netSequential  file programming patterns and performance with .net
Sequential file programming patterns and performance with .netMichael Pavlovsky
 
e-Marke Checkliste für Wohnungen
e-Marke Checkliste für Wohnungene-Marke Checkliste für Wohnungen
e-Marke Checkliste für Wohnungene-Marke Österreich
 
Nuget is easier than you think and you should be using it as both a consumer ...
Nuget is easier than you think and you should be using it as both a consumer ...Nuget is easier than you think and you should be using it as both a consumer ...
Nuget is easier than you think and you should be using it as both a consumer ...Justin James
 

Andere mochten auch (20)

NUnit Features Presentation
NUnit Features PresentationNUnit Features Presentation
NUnit Features Presentation
 
Nunit
NunitNunit
Nunit
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit test
 
Introduction to Unit Testing
Introduction to Unit TestingIntroduction to Unit Testing
Introduction to Unit Testing
 
Unit Test Fun
Unit Test FunUnit Test Fun
Unit Test Fun
 
Nogaaa
NogaaaNogaaa
Nogaaa
 
Comunicaciones katherine ltda
Comunicaciones katherine ltdaComunicaciones katherine ltda
Comunicaciones katherine ltda
 
438lecture1
438lecture1438lecture1
438lecture1
 
Comunicaciones katherine ltda
Comunicaciones katherine ltdaComunicaciones katherine ltda
Comunicaciones katherine ltda
 
Prej Tregueseve të Teuhid-it
Prej Tregueseve të Teuhid-itPrej Tregueseve të Teuhid-it
Prej Tregueseve të Teuhid-it
 
ми6 исследования в интернете2
ми6 исследования в интернете2ми6 исследования в интернете2
ми6 исследования в интернете2
 
An introduction to the Spring Framework
An introduction to the Spring FrameworkAn introduction to the Spring Framework
An introduction to the Spring Framework
 
12th Kuala Lumpur Boys' Brigade Weekly Meeting
12th Kuala Lumpur Boys' Brigade Weekly Meeting12th Kuala Lumpur Boys' Brigade Weekly Meeting
12th Kuala Lumpur Boys' Brigade Weekly Meeting
 
JetBrains ReSharper
JetBrains ReSharperJetBrains ReSharper
JetBrains ReSharper
 
Establecimientos comerciales de la manzana 3
Establecimientos comerciales de la manzana 3Establecimientos comerciales de la manzana 3
Establecimientos comerciales de la manzana 3
 
Safe Creative qué hacemos y cómo lo hacemos
Safe Creative qué hacemos y cómo lo hacemosSafe Creative qué hacemos y cómo lo hacemos
Safe Creative qué hacemos y cómo lo hacemos
 
Estrategia de Desarrollo Local Participativo-AGUJAMA
Estrategia de Desarrollo Local Participativo-AGUJAMAEstrategia de Desarrollo Local Participativo-AGUJAMA
Estrategia de Desarrollo Local Participativo-AGUJAMA
 
Sequential file programming patterns and performance with .net
Sequential  file programming patterns and performance with .netSequential  file programming patterns and performance with .net
Sequential file programming patterns and performance with .net
 
e-Marke Checkliste für Wohnungen
e-Marke Checkliste für Wohnungene-Marke Checkliste für Wohnungen
e-Marke Checkliste für Wohnungen
 
Nuget is easier than you think and you should be using it as both a consumer ...
Nuget is easier than you think and you should be using it as both a consumer ...Nuget is easier than you think and you should be using it as both a consumer ...
Nuget is easier than you think and you should be using it as both a consumer ...
 

Ähnlich wie An Introduction to Unit Test Using NUnit

Automated testing overview
Automated testing overviewAutomated testing overview
Automated testing overviewAlex Pop
 
Winning the battle against Automated testing
Winning the battle against Automated testingWinning the battle against Automated testing
Winning the battle against Automated testingElena Laskavaia
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit TestingSahar Nofal
 
Week 14 Unit Testing.pptx
Week 14  Unit Testing.pptxWeek 14  Unit Testing.pptx
Week 14 Unit Testing.pptxmianshafa
 
Qt test framework
Qt test frameworkQt test framework
Qt test frameworkICS
 
Agile Acceptance testing with Fitnesse
Agile Acceptance testing with FitnesseAgile Acceptance testing with Fitnesse
Agile Acceptance testing with FitnesseClareMcLennan
 
Database Unit Testing Made Easy with VSTS
Database Unit Testing Made Easy with VSTSDatabase Unit Testing Made Easy with VSTS
Database Unit Testing Made Easy with VSTSSanil Mhatre
 
Unit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step TrainingUnit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step TrainingRam Awadh Prasad, PMP
 
DIG1108C Lesson 7 Fall 2014
DIG1108C Lesson 7 Fall 2014DIG1108C Lesson 7 Fall 2014
DIG1108C Lesson 7 Fall 2014David Wolfpaw
 
Becoming a better programmer - unit testing
Becoming a better programmer - unit testingBecoming a better programmer - unit testing
Becoming a better programmer - unit testingDuy Tan Geek
 

Ähnlich wie An Introduction to Unit Test Using NUnit (20)

Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Automated testing overview
Automated testing overviewAutomated testing overview
Automated testing overview
 
Winning the battle against Automated testing
Winning the battle against Automated testingWinning the battle against Automated testing
Winning the battle against Automated testing
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Week 14 Unit Testing.pptx
Week 14  Unit Testing.pptxWeek 14  Unit Testing.pptx
Week 14 Unit Testing.pptx
 
Testing Angular
Testing AngularTesting Angular
Testing Angular
 
Qt test framework
Qt test frameworkQt test framework
Qt test framework
 
Testing
TestingTesting
Testing
 
Agile Acceptance testing with Fitnesse
Agile Acceptance testing with FitnesseAgile Acceptance testing with Fitnesse
Agile Acceptance testing with Fitnesse
 
Database Unit Testing Made Easy with VSTS
Database Unit Testing Made Easy with VSTSDatabase Unit Testing Made Easy with VSTS
Database Unit Testing Made Easy with VSTS
 
Tdd
TddTdd
Tdd
 
Unit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step TrainingUnit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step Training
 
Istqb foundation level day 1
Istqb foundation level   day 1Istqb foundation level   day 1
Istqb foundation level day 1
 
Unit testing in PHP
Unit testing in PHPUnit testing in PHP
Unit testing in PHP
 
DIG1108C Lesson 7 Fall 2014
DIG1108C Lesson 7 Fall 2014DIG1108C Lesson 7 Fall 2014
DIG1108C Lesson 7 Fall 2014
 
Software testing
Software testingSoftware testing
Software testing
 
Software Testing
Software TestingSoftware Testing
Software Testing
 
Manual testing - Introduction to Manual Software testing
Manual testing - Introduction to Manual Software testingManual testing - Introduction to Manual Software testing
Manual testing - Introduction to Manual Software testing
 
Unit Tesing in iOS
Unit Tesing in iOSUnit Tesing in iOS
Unit Tesing in iOS
 
Becoming a better programmer - unit testing
Becoming a better programmer - unit testingBecoming a better programmer - unit testing
Becoming a better programmer - unit testing
 

Kürzlich hochgeladen

Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Will Schroeder
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Websitedgelyza
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxUdaiappa Ramachandran
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxMatsuo Lab
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8DianaGray10
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Adtran
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6DianaGray10
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXTarek Kalaji
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7DianaGray10
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsSafe Software
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UbiTrack UK
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAshyamraj55
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding TeamAdam Moalla
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureEric D. Schabell
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintMahmoud Rabie
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsSeth Reyes
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfJamie (Taka) Wang
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdfPedro Manuel
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarPrecisely
 

Kürzlich hochgeladen (20)

Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Website
 
20230104 - machine vision
20230104 - machine vision20230104 - machine vision
20230104 - machine vision
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptx
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptx
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBX
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability Adventure
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership Blueprint
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and Hazards
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdf
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity Webinar
 

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