0% found this document useful (0 votes)
30 views16 pages

Unit Test

The document outlines a unit testing workshop agenda, covering topics such as the definition of unit tests, effective development practices, and the use of mocking in tests. It emphasizes a good mindset for writing tests, the importance of various testing frameworks, and practical applications of unit testing. The document also includes references for further reading and concludes with a Q&A session.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views16 pages

Unit Test

The document outlines a unit testing workshop agenda, covering topics such as the definition of unit tests, effective development practices, and the use of mocking in tests. It emphasizes a good mindset for writing tests, the importance of various testing frameworks, and practical applications of unit testing. The document also includes references for further reading and concludes with a Q&A session.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Unit Test

Agenda

► Introduction
► Unit Test
► A good mindset
► Develop UT effectively
► Mock Test
► UT in practice
► References
► Q&A
Introduction

Cuong Nguyen
► Senior Software Architect at Cadena Vietnam.
► More than 12 years of experiences in software development.
► Hand-on experiences in a few domains: ERP, HRM, Logistic, E-commerce,
and Healthcare.
► Email: [email protected]
Unit Test

What is Unit Test ?


A type of software testing where individual components or functions of
a program are tested in isolation to ensure they work correctly. These
tests verify that each part of the code behaves as expected and meets
its specified requirements.
► Production codes and Unit Test codes
► individual components or functions
► tested in isolation
► meets its specified requirements
Unit Test (cont)

What to “Unit Test” ?


Unit Test (cont)

Unit testing framework


► For .NET
► NUnit
► xUnit.NET
► For Java
► JUnit
► TestNG
► For front-end
► React Testing Library
► Mocha
Unit Test (cont)
How to create a UT? 3 steps
► Arrange
► Act
► Assert
A good mindset

► Code writing for Unit Test.


► Test first
► Test to find bug / error, not happy test
► The criterias:
► Simple Responsibility, Fast, Independently, readability, maintainability.
► The design principles: SOLID, DRY, KISS, YANI
► Code coverage %
► Consider using TDD (Test Driven Design)
Develop UT effectively

Develop A function - A Unit of work


► Entry point (calling a function)
► Input
► Direct input: parameters
► In-direct input
► Status, behavior
► In-coming dependency (3rd party) (query DB, query from API, ...)

=> Inject object via DI, Interface, Adapter. Not for asserting.
► Calculation / processing
Develop UT effectively (cont)

► Exit points
► Output
► Direct output: parameters
► In-direct output
► Status, behavior
► Outgoing dependency (3rd party) (update DB, update via API, ...)

=> Inject object via DI, Interface, Adapter

► How many UTs for a function?


=> By exit points, combine various cases of input.
Mock Test
What is Mock?
► is an object created to simulate the behavior of a real subsystem in
controlled ways.
► To focus on testing a specific component, we replace its
dependencies with mocks.
► allows us to isolate the behavior of the object under test by
replacing its collaborators with simulate.
Mock Test
Mocking types
► Dummies
► placeholders used to satisfy method signatures or parameter requirements.
► don’t have any real behavior and are often used when a parameter is needed but not
actually used in the test.
► Fakes
► simplified implementations of real components, provide basic functionality but are not
suitable for production use.
► Examples include in-memory databases, file system fakes, or simplified network
services.
► Stubs
► minimal simulated objects, provide just enough behavior to allow the object under
test to execute the test.
► used to replace real dependencies and ensure that the test runs smoothly, don’t verify
interactions.
► Spies
► similar to stubs but also allow you to verify interactions, record information about
method calls (such as arguments passed) for later inspection.
► to check whether specific methods were called or how many times they were called,
spies come in handy.
UT in practice
Improving the quality of codes.
UT in practice
Meaningful naming.
Various asserting cases.
UT in practice
Combine various mocks.
Reference
Unit Testing | What it is, How it Works, Types
What Is Unit Testing? (Definition, Benefits, How-To) | Built In
Testing in .NET - .NET | Microsoft Learn
Unit testing C# with NUnit and .NET Core - .NET
13 Best Java Testing Frameworks [2024]
Q&A
► Any questions ?
► Thank you !!!

You might also like