Real Device Hot Instant access to a wide range of iOS and Android real devices
Automation Hot Perform mobile app automated testing in popular frameworks
PerfDog Hot A client performance testing expert service package

What are Assertions in Unit Testing, and How Does It Help with Unit Testing?

What are assertions in unit testing? When it comes to ensuring the quality and reliability of software, unit testing plays a crucial role. And at the heart of unit testing lies the concept of assertions.

What are assertions in unit testing?

Assertions are an essential component of unit testing. Most unit tests consist of three parts:

  1. Arrangement: Setting up the environment and preparing a set of objects to run the unit under test.
  2. Action: Invoking the unit under test.
  3. Assertion: Checking if the output and side effects of the unit under test match the expected behavior.

These three parts are commonly referred to as the "arrange," "act," and "assert" phases of testing. Other names for these phases include "given/when/then" or even "setup/exercise/verify." The first two parts exercise the code in relevant scenarios, while the assert part ensures that the code behaves as expected.

In the context of unit testing, assertions are statements or conditions that verify specific expectations about the behavior of the code being tested. They are used to validate that the output, state, or side effects of the unit under test meet the expected criteria.

Here are some vivid examples of assertions in unit testing:

  1. Assertion for Expected Output: In a function that adds two numbers, an assertion could be used to verify that calling the function with inputs 3 and 5 results in an output of 8.
  2. Assertion for State Verification: When testing a class method that modifies an internal state, an assertion can be used to ensure that the state has been correctly updated after invoking the method.
  3. Assertion for Exception Handling: If a function is expected to raise a specific exception under certain conditions, an assertion can be used to check whether the exception is raised correctly.

Specific examples of assertions: How to write a better assertion?

Assume we have a class called Calculator with a method ‘add’ that adds two numbers and returns the result. We want to test this method using assertions. Here's an example unit test:


public void testAdd() {

  // Arrange

  Calculator calculator = new Calculator();


  // Act

  int result = calculator.add(2, 3);


  // Assert

  assertEquals(5, result);



In this test, we have a single assertion using the ‘assertEquals’ method from a testing framework (e.g., JUnit). The assertion checks if the result of the addition operation (result) is equal to the expected value of 5.

If the ‘add’ method is correctly implemented and returns the sum of the two input numbers, the assertion will pass. However, if there is a bug in the add method, such as a mistake in the addition logic, the assertion will fail, indicating that the behavior of the code does not match the expected outcome.

By using assertions, we can validate that the code behaves as intended and detect any discrepancies between the expected and actual results. Assertions act as a safety net, helping to identify potential bugs and ensure the correctness of the code under test.

In this example, the assertion ensures that the addition operation is correctly implemented and returns the expected result. If the assertion fails, it indicates that there is an issue with the add method, prompting developers to investigate and fix the problem.

Unit testing in the mobile game development

Unit testing is a key part of game development, which is why we need to delve into assertions. Unit testing in game development is important for ensuring code quality, stability, and bug detection. It enables iterative development, supports collaboration and refactoring, validates game mechanics, and integrates into CI/CD pipelines.

WeTest is a testing solution for mobile game development. While it is not specifically focused on unit testing, it can support unit testing efforts by providing test automation, device compatibility testing, performance testing, user behavior simulation, and integration with testing frameworks.

For example, Tencent WeTest's functional testing can assist unit testing in game development by offering customized use case design, expert verification of functional integrity, and detailed problem localization. With their experienced team and comprehensive reports, WeTest helps developers quickly identify and address functional issues during unit testing.

In conclusion

Assertions are a powerful tool to verify the correctness and expected behavior of code. By defining assertions, developers can set specific expectations about the output and behavior of their code units, enabling them to detect bugs and ensure the desired functionality.

Top Ranking
1End to End Testing vs Integration Testing: Key Differences Today we are going to talk about end-to-end testing vs integration testing and all the related concepts which one needs to know to get started with these testing techniques.
2How to Find Screen Resolution on Android In this blog post, we will talk about the much-asked question of how to find screen resolution on android smartphones and TV along with changing it. Make sure to read till the end to learn this trending query.
3Integration Testing vs Functional Testing: A Quick Look at the Pros and Cons Integration testing vs functional testing, which one is better? They are two essential components of software testing that ensure the quality and reliability of a product.
4How to automate Unity Games using Altunity Tester In today’s competitive mobile market, mobile app development teams have realized that they need to speed up the pace of releases to meet their customer expectations.
5Insight On Special Test of Explosive Games: 51CTO Interview with PerfDog Founder Wensheng Cao Mr. Cao shares with us his practical experience in the field of game testing.