May 16, 2023

Unit Testing (Test Level)

Great job on starting a new lesson! After reading this lesson, click Next 👉 button at bottom right to continue to the next lesson.

Unit testing is a test level in software testing that focuses on an individual unit or component of your software, for example, testing a login method or a testing a calculate method in a class. The purpose of unit testing is to validate that each unit of the software meets the requirements and works as expected. Unit testing is typically performed early, before the code is integrated and the software is tested as a single system.

Unit testing examples

  • Component testing: It is testing a component that provides a specific functionality, such as a button, a menu, or a form. For example, testing if a button changes color when clicked.
  • Module testing: It is testing a module that consists of multiple components that work together to provide a feature, such as a login module, a payment module, or a registration module. For example, testing if the login module validates the user credentials and redirects the validated user to the home page.
  • Mocking and stubbing: It is using mock objects or stubs to simulate the behavior of external dependencies, such as databases, APIs, or services, typically when these external dependencies are not yet available. For example, using a mock database to test a module that queries data from the database.
  • White-box testing: It is testing the internal structure and logic of the code, such as branches, loops, conditions, and statements. For example, testing if a function returns the correct output for different input values.
  • Black-box testing: It is testing the the code while ignoring its internal structure and logic. For example, testing if a method throws an exception for invalid input.
  • Test-driven development (TDD): It involves writing unit tests before writing the code, and then writing the code to make the tests pass. For example, writing a test case for a function that calculates the factorial of a number, and then writing the function to pass the test.
  • Behavior-driven development (BDD): It involves writing unit tests in a natural language that describes the expected behavior of the code. For example, writing and running a test case for a method that checks if a user input is a question using the format: Given an input string, when I check if it is a question, then I should get true or false.
  • Coverage testing: It is measuring how much of the code is covered by the unit tests, such as statements, branches, functions, or lines. For example, using a tool to measure the percentage of code coverage by the unit tests.

Tips for unit testing

  • Use the appropriate unit testing framework, such as JUnit, TestNG, or NUnit, to automate and organize your unit tests.
  • Give clear and descriptive test names and add comments to document your tests.
  • Use mock objects or stubs to isolate the unit under test from external dependencies.
  • Cover both positive and negative test cases. Include edge cases and error conditions respectively.
  • Run your unit tests frequently and fix any failures as soon as possible.

FAQ (interview questions and answers)

  1. What is the main difference between unit testing and integration testing?
    Unit testing tests individual units or components in isolation, while integration testing tests multiple units or components working together.
  2. What is test-driven development (TDD)?
    It is a software development methodology that involves writing unit tests before writing the code, and then writing only the code to make those unit tests pass.
  3. What are the benefits of unit testing to you?
    It helps to find bugs early in the development cycle, and the unit tests provide documentation for the code.
  4. What are some challenges of unit testing?
    It requires effort to write and maintain good unit tests, it may not detect all the defects or cover all the scenarios, it may introduce false positives or negatives, and it may be limited because of the test data and tools.
Remember to just comment if you have any doubts or queries.



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.