Introduction
Unit testing is a method for testing individual units or components of code to ensure they behave as expected. It’s essential for catching bugs early and verifying the functionality of specific functions or methods. In this course, you’ll learn how to create and run unit tests in JavaScript using Jest and in C# using xUnit.
1. JavaScript Unit Testing Example with Jest
Step-by-Step Guide
-
Setup:
-
Initialize a new Node.js project and install Jest with the following commands:
npm init -y npm install --save-dev jest
-
Add a test script to
package.json
to enable test execution:"scripts": { "test": "jest" }
-
-
Code to Test:
- Create a file named
math.js
:// math.js function add(a, b) { return a + b; } module.exports = add;
- Create a file named
-
Write the Unit Test:
- Create a file named
math.test.js
:// math.test.js const add = require('./math'); test('adds 1 + 2 to equal 3', () => { expect(add(1, 2)).toBe(3); }); test('adds 5 + 10 to equal 15', () => { expect(add(5, 10)).toBe(15); });
- Create a file named
-
Run the Test:
- In the terminal, run:
npm test
- Jest will display output indicating the success of each test.
- In the terminal, run:
2. C# Unit Testing Example with xUnit
Step-by-Step Guide
-
Setup:
- Create a new C# xUnit project:
dotnet new xunit -o UnitTestingExample cd UnitTestingExample
- Create a new C# xUnit project:
-
Code to Test:
- Add a file called
Calculator.cs
:// Calculator.cs public class Calculator { public int Add(int a, int b) { return a + b; } }
- Add a file called
-
Write the Unit Test:
- Add a file called
CalculatorTests.cs
:// CalculatorTests.cs using Xunit; public class CalculatorTests { [Fact] public void Add_WhenAdding1And2_ShouldReturn3() { // Arrange var calculator = new Calculator(); // Act int result = calculator.Add(1, 2); // Assert Assert.Equal(3, result); } [Fact] public void Add_WhenAdding5And10_ShouldReturn15() { // Arrange var calculator = new Calculator(); // Act int result = calculator.Add(5, 10); // Assert Assert.Equal(15, result); } }
- Add a file called
-
Run the Test:
- Use the command below to run tests:
dotnet test
- xUnit will output the results, indicating whether each test has passed.
- Use the command below to run tests:
Quiz: Unit Testing Knowledge Check
Test your understanding of the concepts covered with this quiz.
-
What is the main purpose of unit testing?
- To test the entire application’s behavior
- To test individual functions or methods for correct behavior
- To check for UI responsiveness
- To test API interactions only
-
Which of the following commands runs Jest tests in JavaScript?
-
node test
-
npm test
-
jest start
-
run jest
-
-
What is the primary difference between xUnit and Jest?
- xUnit is for Python, and Jest is for JavaScript
- xUnit is for web development, and Jest is for mobile apps
- xUnit is commonly used with .NET/C#, while Jest is used with JavaScript
- They are the same, just used on different platforms
-
In the C# example, which library provides the testing framework?
- NUnit
- xUnit
- JUnit
- MSTest
Summary
In this lesson, you’ve learned how to write basic unit tests in both JavaScript and C#. You practiced setting up a project, writing test cases, and running them to verify correct behavior. With this foundation, you can explore more complex testing strategies and frameworks.