SlideShare a Scribd company logo
CloudCube Software Solutions
Beginners -
Get started with Unit Testing in .NET
Email : ContactUs@CloudCubeSoftware.com
WebSite : www.CloudCubeSoftware.com
GET IN TOUCH FOR FREE WEBINAR
What is Unit Test?
● Unit – Smallest piece of testable code (like
functions, method, procedure and class)
● Unit Testing – Process of testing the
correctness of each testable unit of code by
isolating it from external dependencies.
Why Unit Testing?
● Helps to eliminate the bug during the
development stage
● Helps to reproduce production bugs
● Helps to debug the code
● Helps to re-factor the code with confidence
● Helps to improve the code coverage
● Helps to improve the code and design
● Helps to automate the testing process
Unit Testing Frameworks for .NET
● NUnit – Open Source Unit Testing framework for .NET
initially ported from JUnit. URL: http://www.nunit.org/
● xUnit.net – Open Source Unit Testing framework written by
original inventor of NUnit v2. URL: https://xunit.github.io/
● Visual Studio Unit Testing Framework – Reference the
namespace “Microsoft.VisualStudio.TestTools.UnitTesting”
● Gallio/MbUnit – Unit Testing Framework for .NET. Note:
mbunit-v3 last released in Aug-2013.
URL: https://github.com/Gallio and
https://code.google.com/archive/p/mb-unit/
Visual Studio Unit Testing
Introduction to Unit Testing
using
Visual Studio 2015
Create a Class Library project
● Create a C# Class Library project
New > Project > Templates > Visual C# > Windows > Class Library
●
Add Unit Test Project
● Create a new unit test project
Add New Project > Templates > Visual C# > Test > Unit Test Project
● Namespace : Microsoft.VisualStudio.TestTools.UnitTesting
● Attribute - [TestClass]
● Attribute - [Test Method]
Arrange, Act and Assert - AAA
● Reference the Class Library Project in the Unit Test Project
● AAA – Arrange, Act and Assert in [TestMethod]
How to Run Unit Tests?
● Open Test Explorer – Test > Windows > Test Explorer
● Test Explorer - Click Run All
How to test exception?
● Attribute - [ExpectedException]
● CalculateAvgSalary method
● Test Method
Additional Test Attributes
● [ClassInitialize], [ClassCleanup], [TestInitialize], [TestCleanup]
Introduction to NUnit
Introduction to Unit Testing
using
NUnit
Add Unit Test Project
● Add a new C# Class Library “SampleLibrary.NunitTests”
Add New Project > Templates > Visual C# > Windows > Class Library
● Rename the class Class1.cs as “SalaryCalculatorTest.cs”
Open NuGet package Manager
● Right Click Project > Manage NuGet Packages
Install NUnit
● Install NUnit
NUnit References
● NUnit References
Add NUnit – Unit Test Method
● Equivalent of [TestClass] is [TestFixture]
● Equivalent of [TestMethod] is [Test]
Install NUnit 3 Test Adapter
● Go to Tools >Extensions and Updates > Online
● Search for NUnit 3 Test Adapter > Download
Run the NUnit – Test Cases
● Go to Test > Windows > Test Explorer and click Run All
xUnit.net - Unit Testing
Introduction to Unit Testing
using
xUnit.net
Create Unit Test Project
● Create a C# Class Library “SampleLibrary.XunitTests”
Add New Project > Templates > Visual C# > Windows > Class Library
● Rename the class Class1.cs as “SalaryCalculatorTest.cs”
Open NuGet package Manager
● Right click project > “Manage NuGet Packages”
Install xUnit.net
● Install xUnit.net
xUnit.net - References
● References made by xUnit.net
Create xUnit – Unit Test
● Decorate the test method with [Fact]
Add xunit.runner.visualstudio
● Go to NuGet Package manager
● Add xunit.runner.visualstudio
Run the xUnit – Unit Tests
● Go to Test > Windows > Test Explorer and click Run All
Mocking
Introduction to Mocking
What is Mocking?
● Unit testing aims to test the smallest possible
testable unit of code by isolating external
dependencies (like calls to database, web
services, email service etc)
● Mocking helps to replace the external
dependencies while unit testing
● Mocks, Stubs and Fakes are used in Mocking
Advantages of Mocking
● Helps to run the unit tests faster by reducing
the execution time
● Helps to write the unit tests without depending
on external services / components
– No need to setup the database to unit test the code
dependent on database access layer (DAL).
– No need to setup the real service to unit test the code
dependent on service layer
– No need to setup the SMTP / email server to unit test the
code dependent on SMTP and mail server.
Mocking Frameworks for .NET
● Moq - Simple to use, strong typed and minimalistic mocking framework
URL: https://github.com/Moq/moq4/wiki/Quickstart
● FakeItEasy – The easy mocking library for .NET
https://fakeiteasy.github.io/
● NSubstitute - A friendly substitute for .NET mocking framework URL:
http://nsubstitute.github.io/
●
Rhino Mocks - Dynamic mock object framework for the .Net platform. URL:
https://www.hibernatingrhinos.com/oss/rhino-mocks
● JustMock - .NET Mocking framework from telerik URL:
http://www.telerik.com/products/mocking.aspx
● Typemock – Smart Unit testing Solutions for .NET URL:
http://www.typemock.com/
What is TDD / TFD ?
● TDD – Test-driven Development
● Write unit test before writing fully functional
code (Test-First Development - TFD)
● Unit test fails to start with
● Write the code to satisfy and pass the test
case.
Thank you
HOPE YOU ENJOYED
DO YOU HAVE ANY QUESTIONS?
WANT TO LEARN NUnit or xUnit.net?
WANT TO LEARN ABOUT MOCKING?
GET IN TOUCH FOR FREE WEBINAR
ContactUs@CloudCubeSoftware.com
www.cloudcubesoftware.com

More Related Content

What's hot

Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And MockingJoe Wilson
 
Unit Test Presentation
Unit Test PresentationUnit Test Presentation
Unit Test PresentationSayedur Rahman
 
Unit testing with NUnit
Unit testing with NUnitUnit testing with NUnit
Unit testing with NUnit
kleinron
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
Joe Tremblay
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPTsuhasreddy1
 
Test automation
Test automationTest automation
Test automation
Xavier Yin
 
Unit testing
Unit testingUnit testing
Unit testing
princezzlove
 
Automated Testing vs Manual Testing
Automated Testing vs Manual TestingAutomated Testing vs Manual Testing
Automated Testing vs Manual Testing
didev
 
Selenium with Cucumber
Selenium  with Cucumber Selenium  with Cucumber
Selenium with Cucumber
Knoldus Inc.
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
Nayanda Haberty
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
Sergey Podolsky
 
Keyword-driven Test Automation Framework
Keyword-driven Test Automation FrameworkKeyword-driven Test Automation Framework
Keyword-driven Test Automation Framework
Mikhail Subach
 
BDD with Cucumber
BDD with CucumberBDD with Cucumber
BDD with Cucumber
Knoldus Inc.
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in Angular
Knoldus Inc.
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
priya_trivedi
 
Unit Testing with xUnit.net - Part 2
Unit Testing with xUnit.net - Part 2Unit Testing with xUnit.net - Part 2
Unit Testing with xUnit.net - Part 2
BizTalk360
 
Workshop unit test
Workshop   unit testWorkshop   unit test
Workshop unit test
Francesco Garavaglia
 
Best Practices for Test Case Writing
Best Practices for Test Case WritingBest Practices for Test Case Writing
Best Practices for Test Case WritingSarah Goldberg
 

What's hot (20)

Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
Unit Test Presentation
Unit Test PresentationUnit Test Presentation
Unit Test Presentation
 
Unit testing with NUnit
Unit testing with NUnitUnit testing with NUnit
Unit testing with NUnit
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
xUnit
xUnitxUnit
xUnit
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
 
Test automation
Test automationTest automation
Test automation
 
Unit testing
Unit testingUnit testing
Unit testing
 
Automated Testing vs Manual Testing
Automated Testing vs Manual TestingAutomated Testing vs Manual Testing
Automated Testing vs Manual Testing
 
Nunit
NunitNunit
Nunit
 
Selenium with Cucumber
Selenium  with Cucumber Selenium  with Cucumber
Selenium with Cucumber
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Keyword-driven Test Automation Framework
Keyword-driven Test Automation FrameworkKeyword-driven Test Automation Framework
Keyword-driven Test Automation Framework
 
BDD with Cucumber
BDD with CucumberBDD with Cucumber
BDD with Cucumber
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in Angular
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
Unit Testing with xUnit.net - Part 2
Unit Testing with xUnit.net - Part 2Unit Testing with xUnit.net - Part 2
Unit Testing with xUnit.net - Part 2
 
Workshop unit test
Workshop   unit testWorkshop   unit test
Workshop unit test
 
Best Practices for Test Case Writing
Best Practices for Test Case WritingBest Practices for Test Case Writing
Best Practices for Test Case Writing
 

Similar to Beginners - Get Started With Unit Testing in .NET

Unit testing
Unit testing Unit testing
Unit testing
dubbu
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
Roman Okolovich
 
Unit testing (eng)
Unit testing (eng)Unit testing (eng)
Unit testing (eng)
Anatoliy Okhotnikov
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
Facundo Farias
 
Unit testing in Unity
Unit testing in UnityUnit testing in Unity
Unit testing in Unity
Mikko McMenamin
 
Test Driven Development with Sql Server
Test Driven Development with Sql ServerTest Driven Development with Sql Server
Test Driven Development with Sql Server
David P. Moore
 
Unit testing framework
Unit testing frameworkUnit testing framework
Unit testing framework
Igor Vavrish
 
Automated testing overview
Automated testing overviewAutomated testing overview
Automated testing overview
Alex Pop
 
Automated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra SolutionsAutomated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra Solutions
Quontra Solutions
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + Eclipse
UTC Fire & Security
 
Agile Engineering Sparker GLASScon 2015
Agile Engineering Sparker GLASScon 2015Agile Engineering Sparker GLASScon 2015
Agile Engineering Sparker GLASScon 2015
Stephen Ritchie
 
Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++
Hong Le Van
 
Python and test
Python and testPython and test
Python and test
Micron Technology
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing FundamentalsRichard Paul
 
5 Best Unit Test Frameworks to Automate Unit Tests
5 Best Unit Test Frameworks to Automate Unit Tests5 Best Unit Test Frameworks to Automate Unit Tests
5 Best Unit Test Frameworks to Automate Unit Tests
Serena Gray
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test Framework
Peter Kofler
 
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex ScenariosUnit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Flutter Agency
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testing
Adam Stephensen
 

Similar to Beginners - Get Started With Unit Testing in .NET (20)

Unit testing
Unit testing Unit testing
Unit testing
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
 
Unit testing (eng)
Unit testing (eng)Unit testing (eng)
Unit testing (eng)
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
Unit testing in Unity
Unit testing in UnityUnit testing in Unity
Unit testing in Unity
 
Test Driven Development with Sql Server
Test Driven Development with Sql ServerTest Driven Development with Sql Server
Test Driven Development with Sql Server
 
Unit testing framework
Unit testing frameworkUnit testing framework
Unit testing framework
 
Quality for developers
Quality for developersQuality for developers
Quality for developers
 
Automated testing overview
Automated testing overviewAutomated testing overview
Automated testing overview
 
Automated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra SolutionsAutomated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra Solutions
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + Eclipse
 
Agile Engineering Sparker GLASScon 2015
Agile Engineering Sparker GLASScon 2015Agile Engineering Sparker GLASScon 2015
Agile Engineering Sparker GLASScon 2015
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++
 
Python and test
Python and testPython and test
Python and test
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
 
5 Best Unit Test Frameworks to Automate Unit Tests
5 Best Unit Test Frameworks to Automate Unit Tests5 Best Unit Test Frameworks to Automate Unit Tests
5 Best Unit Test Frameworks to Automate Unit Tests
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test Framework
 
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex ScenariosUnit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testing
 

Recently uploaded

In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Hivelance Technology
 
Why React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdfWhy React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdf
ayushiqss
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
NaapbooksPrivateLimi
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 

Recently uploaded (20)

In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
 
Why React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdfWhy React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdf
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 

Beginners - Get Started With Unit Testing in .NET

  • 1. CloudCube Software Solutions Beginners - Get started with Unit Testing in .NET Email : ContactUs@CloudCubeSoftware.com WebSite : www.CloudCubeSoftware.com GET IN TOUCH FOR FREE WEBINAR
  • 2. What is Unit Test? ● Unit – Smallest piece of testable code (like functions, method, procedure and class) ● Unit Testing – Process of testing the correctness of each testable unit of code by isolating it from external dependencies.
  • 3. Why Unit Testing? ● Helps to eliminate the bug during the development stage ● Helps to reproduce production bugs ● Helps to debug the code ● Helps to re-factor the code with confidence ● Helps to improve the code coverage ● Helps to improve the code and design ● Helps to automate the testing process
  • 4. Unit Testing Frameworks for .NET ● NUnit – Open Source Unit Testing framework for .NET initially ported from JUnit. URL: http://www.nunit.org/ ● xUnit.net – Open Source Unit Testing framework written by original inventor of NUnit v2. URL: https://xunit.github.io/ ● Visual Studio Unit Testing Framework – Reference the namespace “Microsoft.VisualStudio.TestTools.UnitTesting” ● Gallio/MbUnit – Unit Testing Framework for .NET. Note: mbunit-v3 last released in Aug-2013. URL: https://github.com/Gallio and https://code.google.com/archive/p/mb-unit/
  • 5. Visual Studio Unit Testing Introduction to Unit Testing using Visual Studio 2015
  • 6. Create a Class Library project ● Create a C# Class Library project New > Project > Templates > Visual C# > Windows > Class Library ●
  • 7. Add Unit Test Project ● Create a new unit test project Add New Project > Templates > Visual C# > Test > Unit Test Project ● Namespace : Microsoft.VisualStudio.TestTools.UnitTesting ● Attribute - [TestClass] ● Attribute - [Test Method]
  • 8. Arrange, Act and Assert - AAA ● Reference the Class Library Project in the Unit Test Project ● AAA – Arrange, Act and Assert in [TestMethod]
  • 9. How to Run Unit Tests? ● Open Test Explorer – Test > Windows > Test Explorer ● Test Explorer - Click Run All
  • 10. How to test exception? ● Attribute - [ExpectedException] ● CalculateAvgSalary method ● Test Method
  • 11. Additional Test Attributes ● [ClassInitialize], [ClassCleanup], [TestInitialize], [TestCleanup]
  • 12. Introduction to NUnit Introduction to Unit Testing using NUnit
  • 13. Add Unit Test Project ● Add a new C# Class Library “SampleLibrary.NunitTests” Add New Project > Templates > Visual C# > Windows > Class Library ● Rename the class Class1.cs as “SalaryCalculatorTest.cs”
  • 14. Open NuGet package Manager ● Right Click Project > Manage NuGet Packages
  • 17. Add NUnit – Unit Test Method ● Equivalent of [TestClass] is [TestFixture] ● Equivalent of [TestMethod] is [Test]
  • 18. Install NUnit 3 Test Adapter ● Go to Tools >Extensions and Updates > Online ● Search for NUnit 3 Test Adapter > Download
  • 19. Run the NUnit – Test Cases ● Go to Test > Windows > Test Explorer and click Run All
  • 20. xUnit.net - Unit Testing Introduction to Unit Testing using xUnit.net
  • 21. Create Unit Test Project ● Create a C# Class Library “SampleLibrary.XunitTests” Add New Project > Templates > Visual C# > Windows > Class Library ● Rename the class Class1.cs as “SalaryCalculatorTest.cs”
  • 22. Open NuGet package Manager ● Right click project > “Manage NuGet Packages”
  • 24. xUnit.net - References ● References made by xUnit.net
  • 25. Create xUnit – Unit Test ● Decorate the test method with [Fact]
  • 26. Add xunit.runner.visualstudio ● Go to NuGet Package manager ● Add xunit.runner.visualstudio
  • 27. Run the xUnit – Unit Tests ● Go to Test > Windows > Test Explorer and click Run All
  • 29. What is Mocking? ● Unit testing aims to test the smallest possible testable unit of code by isolating external dependencies (like calls to database, web services, email service etc) ● Mocking helps to replace the external dependencies while unit testing ● Mocks, Stubs and Fakes are used in Mocking
  • 30. Advantages of Mocking ● Helps to run the unit tests faster by reducing the execution time ● Helps to write the unit tests without depending on external services / components – No need to setup the database to unit test the code dependent on database access layer (DAL). – No need to setup the real service to unit test the code dependent on service layer – No need to setup the SMTP / email server to unit test the code dependent on SMTP and mail server.
  • 31. Mocking Frameworks for .NET ● Moq - Simple to use, strong typed and minimalistic mocking framework URL: https://github.com/Moq/moq4/wiki/Quickstart ● FakeItEasy – The easy mocking library for .NET https://fakeiteasy.github.io/ ● NSubstitute - A friendly substitute for .NET mocking framework URL: http://nsubstitute.github.io/ ● Rhino Mocks - Dynamic mock object framework for the .Net platform. URL: https://www.hibernatingrhinos.com/oss/rhino-mocks ● JustMock - .NET Mocking framework from telerik URL: http://www.telerik.com/products/mocking.aspx ● Typemock – Smart Unit testing Solutions for .NET URL: http://www.typemock.com/
  • 32. What is TDD / TFD ? ● TDD – Test-driven Development ● Write unit test before writing fully functional code (Test-First Development - TFD) ● Unit test fails to start with ● Write the code to satisfy and pass the test case.
  • 33. Thank you HOPE YOU ENJOYED DO YOU HAVE ANY QUESTIONS? WANT TO LEARN NUnit or xUnit.net? WANT TO LEARN ABOUT MOCKING? GET IN TOUCH FOR FREE WEBINAR ContactUs@CloudCubeSoftware.com www.cloudcubesoftware.com