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 (20)

Unit Testing
Unit TestingUnit Testing
Unit Testing
Sergey Podolsky
 
Introduction to Selenium Web Driver
Introduction to Selenium Web DriverIntroduction to Selenium Web Driver
Introduction to Selenium Web Driver
Return on Intelligence
 
Junit
JunitJunit
Junit
FAROOK Samath
 
Unit Testing (C#)
Unit Testing (C#)Unit Testing (C#)
Unit Testing (C#)
Prashant Cholachagudd
 
Automated Testing vs Manual Testing
Automated Testing vs Manual TestingAutomated Testing vs Manual Testing
Automated Testing vs Manual Testing
didev
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
Dror Helper
 
Unit Testing Using N Unit
Unit Testing Using N UnitUnit Testing Using N Unit
Unit Testing Using N Unit
Gaurav Arora
 
testng
testngtestng
testng
harithakannan
 
Selenium WebDriver
Selenium WebDriverSelenium WebDriver
Selenium WebDriver
Yuriy Bezgachnyuk
 
API Testing for everyone.pptx
API Testing for everyone.pptxAPI Testing for everyone.pptx
API Testing for everyone.pptx
Pricilla Bilavendran
 
Unit testing
Unit testingUnit testing
Unit testing
Slideshare
 
Create an architecture for web test automation
Create an architecture for web test automationCreate an architecture for web test automation
Create an architecture for web test automation
Elias Nogueira
 
Test Automation - Keytorc Approach
Test Automation - Keytorc Approach Test Automation - Keytorc Approach
Test Automation - Keytorc Approach
Keytorc Software Testing Services
 
Automation Testing by Selenium Web Driver
Automation Testing by Selenium Web DriverAutomation Testing by Selenium Web Driver
Automation Testing by Selenium Web Driver
Cuelogic Technologies Pvt. Ltd.
 
Selenium- A Software Testing Tool
Selenium- A Software Testing ToolSelenium- A Software Testing Tool
Selenium- A Software Testing Tool
Zeba Tahseen
 
Test automation
Test automationTest automation
Test automation
Xavier Yin
 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing Framework
Onkar Deshpande
 
Automation Testing with Test Complete
Automation Testing with Test CompleteAutomation Testing with Test Complete
Automation Testing with Test Complete
Vartika Saxena
 
Selenium ppt
Selenium pptSelenium ppt
Selenium ppt
Naga Dinesh
 
Unit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and KarmaUnit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and Karma
Andrey Kolodnitsky
 

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

Skillwise Unit Testing
Skillwise Unit TestingSkillwise Unit Testing
Skillwise Unit Testing
Skillwise Group
 
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxUnit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptx
Knoldus Inc.
 
Unit Testing in Action - C#, NUnit, and Moq
Unit Testing in Action - C#, NUnit, and MoqUnit Testing in Action - C#, NUnit, and Moq
Unit Testing in Action - C#, NUnit, and Moq
XPDays
 
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
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
Rob Hale
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
Roman Okolovich
 
Mini training - Moving to xUnit.net
Mini training - Moving to xUnit.netMini training - Moving to xUnit.net
Mini training - Moving to xUnit.net
Betclic Everest Group Tech Team
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
Sahar Nofal
 
Implementing TDD in for .net Core applications
Implementing TDD in for .net Core applicationsImplementing TDD in for .net Core applications
Implementing TDD in for .net Core applications
Ahmad Kazemi
 
Introduction to Unit Tests and TDD
Introduction to Unit Tests and TDDIntroduction to Unit Tests and TDD
Introduction to Unit Tests and TDD
Betclic Everest Group Tech Team
 
Rc2010 tdd
Rc2010 tddRc2010 tdd
Rc2010 tdd
JasonOffutt
 
Unit testing
Unit testing Unit testing
Unit testing
dubbu
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
CodeOps Technologies LLP
 
N Unit Presentation
N Unit PresentationN Unit Presentation
N Unit Presentation
priya_trivedi
 
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
Uma Ghotikar
 
Binary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code TestingBinary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code Testing
Binary Studio
 
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Ortus Solutions, Corp
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
François Camus
 
Unit testing
Unit testingUnit testing
Unit testing
Abhinav Gujjar
 
RIA 06 & 07 - Unit Testing in Detail
RIA 06 & 07 - Unit Testing in DetailRIA 06 & 07 - Unit Testing in Detail
RIA 06 & 07 - Unit Testing in Detail
Johannes Hoppe
 
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxUnit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptx
Knoldus Inc.
 
Unit Testing in Action - C#, NUnit, and Moq
Unit Testing in Action - C#, NUnit, and MoqUnit Testing in Action - C#, NUnit, and Moq
Unit Testing in Action - C#, NUnit, and Moq
XPDays
 
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
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
Rob Hale
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
Sahar Nofal
 
Implementing TDD in for .net Core applications
Implementing TDD in for .net Core applicationsImplementing TDD in for .net Core applications
Implementing TDD in for .net Core applications
Ahmad Kazemi
 
Unit testing
Unit testing Unit testing
Unit testing
dubbu
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
CodeOps Technologies LLP
 
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
Uma Ghotikar
 
Binary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code TestingBinary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code Testing
Binary Studio
 
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Ortus Solutions, Corp
 
RIA 06 & 07 - Unit Testing in Detail
RIA 06 & 07 - Unit Testing in DetailRIA 06 & 07 - Unit Testing in Detail
RIA 06 & 07 - Unit Testing in Detail
Johannes Hoppe
 
Ad

Recently uploaded (20)

The rise of e-commerce has redefined how retailers operate—and reconciliation...
The rise of e-commerce has redefined how retailers operate—and reconciliation...The rise of e-commerce has redefined how retailers operate—and reconciliation...
The rise of e-commerce has redefined how retailers operate—and reconciliation...
Prachi Desai
 
Custom Software Development: Types, Applications and Benefits.pdf
Custom Software Development: Types, Applications and Benefits.pdfCustom Software Development: Types, Applications and Benefits.pdf
Custom Software Development: Types, Applications and Benefits.pdf
Digital Aptech
 
Topic 26 Security Testing Considerations.pptx
Topic 26 Security Testing Considerations.pptxTopic 26 Security Testing Considerations.pptx
Topic 26 Security Testing Considerations.pptx
marutnand8
 
Autoposting.ai Sales Deck - Skyrocket your LinkedIn's ROI
Autoposting.ai Sales Deck - Skyrocket your LinkedIn's ROIAutoposting.ai Sales Deck - Skyrocket your LinkedIn's ROI
Autoposting.ai Sales Deck - Skyrocket your LinkedIn's ROI
Udit Goenka
 
Facility Management Solution - TeroTAM CMMS Software
Facility Management Solution - TeroTAM CMMS SoftwareFacility Management Solution - TeroTAM CMMS Software
Facility Management Solution - TeroTAM CMMS Software
TeroTAM
 
Secure and Simplify IT Management with ManageEngine Endpoint Central.pdf
Secure and Simplify IT Management with ManageEngine Endpoint Central.pdfSecure and Simplify IT Management with ManageEngine Endpoint Central.pdf
Secure and Simplify IT Management with ManageEngine Endpoint Central.pdf
Northwind Technologies
 
How AI Can Improve Media Quality Testing Across Platforms (1).pptx
How AI Can Improve Media Quality Testing Across Platforms (1).pptxHow AI Can Improve Media Quality Testing Across Platforms (1).pptx
How AI Can Improve Media Quality Testing Across Platforms (1).pptx
kalichargn70th171
 
Top 10 Mobile Banking Apps in the USA.pdf
Top 10 Mobile Banking Apps in the USA.pdfTop 10 Mobile Banking Apps in the USA.pdf
Top 10 Mobile Banking Apps in the USA.pdf
LL Technolab
 
Build enterprise-ready applications using skills you already have!
Build enterprise-ready applications using skills you already have!Build enterprise-ready applications using skills you already have!
Build enterprise-ready applications using skills you already have!
PhilMeredith3
 
How a Staff Augmentation Company IN USA Powers Flutter App Breakthroughs.pdf
How a Staff Augmentation Company IN USA Powers Flutter App Breakthroughs.pdfHow a Staff Augmentation Company IN USA Powers Flutter App Breakthroughs.pdf
How a Staff Augmentation Company IN USA Powers Flutter App Breakthroughs.pdf
mary rojas
 
Shortcomings of EHS Software – And How to Overcome Them
Shortcomings of EHS Software – And How to Overcome ThemShortcomings of EHS Software – And How to Overcome Them
Shortcomings of EHS Software – And How to Overcome Them
TECH EHS Solution
 
Delivering More with Less: AI Driven Resource Management with OnePlan
Delivering More with Less: AI Driven Resource Management with OnePlan Delivering More with Less: AI Driven Resource Management with OnePlan
Delivering More with Less: AI Driven Resource Management with OnePlan
OnePlan Solutions
 
Micro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Micro-Metrics Every Performance Engineer Should Validate Before Sign-OffMicro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Micro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Tier1 app
 
iOS Developer Resume 2025 | Pramod Kumar
iOS Developer Resume 2025 | Pramod KumariOS Developer Resume 2025 | Pramod Kumar
iOS Developer Resume 2025 | Pramod Kumar
Pramod Kumar
 
Scalefusion Remote Access for Apple Devices
Scalefusion Remote Access for Apple DevicesScalefusion Remote Access for Apple Devices
Scalefusion Remote Access for Apple Devices
Scalefusion
 
Software Risk and Quality management.pptx
Software Risk and Quality management.pptxSoftware Risk and Quality management.pptx
Software Risk and Quality management.pptx
HassanBangash9
 
War Story: Removing Offensive Language from Percona Toolkit
War Story: Removing Offensive Language from Percona ToolkitWar Story: Removing Offensive Language from Percona Toolkit
War Story: Removing Offensive Language from Percona Toolkit
Sveta Smirnova
 
AI Alternative - Discover the best AI tools and their alternatives
AI Alternative - Discover the best AI tools and their alternativesAI Alternative - Discover the best AI tools and their alternatives
AI Alternative - Discover the best AI tools and their alternatives
AI Alternative
 
Issues in AI Presentation and machine learning.pptx
Issues in AI Presentation and machine learning.pptxIssues in AI Presentation and machine learning.pptx
Issues in AI Presentation and machine learning.pptx
Jalalkhan657136
 
zOS CommServer support for the Network Express feature on z17
zOS CommServer support for the Network Express feature on z17zOS CommServer support for the Network Express feature on z17
zOS CommServer support for the Network Express feature on z17
zOSCommserver
 
The rise of e-commerce has redefined how retailers operate—and reconciliation...
The rise of e-commerce has redefined how retailers operate—and reconciliation...The rise of e-commerce has redefined how retailers operate—and reconciliation...
The rise of e-commerce has redefined how retailers operate—and reconciliation...
Prachi Desai
 
Custom Software Development: Types, Applications and Benefits.pdf
Custom Software Development: Types, Applications and Benefits.pdfCustom Software Development: Types, Applications and Benefits.pdf
Custom Software Development: Types, Applications and Benefits.pdf
Digital Aptech
 
Topic 26 Security Testing Considerations.pptx
Topic 26 Security Testing Considerations.pptxTopic 26 Security Testing Considerations.pptx
Topic 26 Security Testing Considerations.pptx
marutnand8
 
Autoposting.ai Sales Deck - Skyrocket your LinkedIn's ROI
Autoposting.ai Sales Deck - Skyrocket your LinkedIn's ROIAutoposting.ai Sales Deck - Skyrocket your LinkedIn's ROI
Autoposting.ai Sales Deck - Skyrocket your LinkedIn's ROI
Udit Goenka
 
Facility Management Solution - TeroTAM CMMS Software
Facility Management Solution - TeroTAM CMMS SoftwareFacility Management Solution - TeroTAM CMMS Software
Facility Management Solution - TeroTAM CMMS Software
TeroTAM
 
Secure and Simplify IT Management with ManageEngine Endpoint Central.pdf
Secure and Simplify IT Management with ManageEngine Endpoint Central.pdfSecure and Simplify IT Management with ManageEngine Endpoint Central.pdf
Secure and Simplify IT Management with ManageEngine Endpoint Central.pdf
Northwind Technologies
 
How AI Can Improve Media Quality Testing Across Platforms (1).pptx
How AI Can Improve Media Quality Testing Across Platforms (1).pptxHow AI Can Improve Media Quality Testing Across Platforms (1).pptx
How AI Can Improve Media Quality Testing Across Platforms (1).pptx
kalichargn70th171
 
Top 10 Mobile Banking Apps in the USA.pdf
Top 10 Mobile Banking Apps in the USA.pdfTop 10 Mobile Banking Apps in the USA.pdf
Top 10 Mobile Banking Apps in the USA.pdf
LL Technolab
 
Build enterprise-ready applications using skills you already have!
Build enterprise-ready applications using skills you already have!Build enterprise-ready applications using skills you already have!
Build enterprise-ready applications using skills you already have!
PhilMeredith3
 
How a Staff Augmentation Company IN USA Powers Flutter App Breakthroughs.pdf
How a Staff Augmentation Company IN USA Powers Flutter App Breakthroughs.pdfHow a Staff Augmentation Company IN USA Powers Flutter App Breakthroughs.pdf
How a Staff Augmentation Company IN USA Powers Flutter App Breakthroughs.pdf
mary rojas
 
Shortcomings of EHS Software – And How to Overcome Them
Shortcomings of EHS Software – And How to Overcome ThemShortcomings of EHS Software – And How to Overcome Them
Shortcomings of EHS Software – And How to Overcome Them
TECH EHS Solution
 
Delivering More with Less: AI Driven Resource Management with OnePlan
Delivering More with Less: AI Driven Resource Management with OnePlan Delivering More with Less: AI Driven Resource Management with OnePlan
Delivering More with Less: AI Driven Resource Management with OnePlan
OnePlan Solutions
 
Micro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Micro-Metrics Every Performance Engineer Should Validate Before Sign-OffMicro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Micro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Tier1 app
 
iOS Developer Resume 2025 | Pramod Kumar
iOS Developer Resume 2025 | Pramod KumariOS Developer Resume 2025 | Pramod Kumar
iOS Developer Resume 2025 | Pramod Kumar
Pramod Kumar
 
Scalefusion Remote Access for Apple Devices
Scalefusion Remote Access for Apple DevicesScalefusion Remote Access for Apple Devices
Scalefusion Remote Access for Apple Devices
Scalefusion
 
Software Risk and Quality management.pptx
Software Risk and Quality management.pptxSoftware Risk and Quality management.pptx
Software Risk and Quality management.pptx
HassanBangash9
 
War Story: Removing Offensive Language from Percona Toolkit
War Story: Removing Offensive Language from Percona ToolkitWar Story: Removing Offensive Language from Percona Toolkit
War Story: Removing Offensive Language from Percona Toolkit
Sveta Smirnova
 
AI Alternative - Discover the best AI tools and their alternatives
AI Alternative - Discover the best AI tools and their alternativesAI Alternative - Discover the best AI tools and their alternatives
AI Alternative - Discover the best AI tools and their alternatives
AI Alternative
 
Issues in AI Presentation and machine learning.pptx
Issues in AI Presentation and machine learning.pptxIssues in AI Presentation and machine learning.pptx
Issues in AI Presentation and machine learning.pptx
Jalalkhan657136
 
zOS CommServer support for the Network Express feature on z17
zOS CommServer support for the Network Express feature on z17zOS CommServer support for the Network Express feature on z17
zOS CommServer support for the Network Express feature on z17
zOSCommserver
 
Ad

Beginners - Get Started With Unit Testing in .NET

  • 1. CloudCube Software Solutions Beginners - Get started with Unit Testing in .NET Email : [email protected] 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 [email protected] www.cloudcubesoftware.com