Backend Low Level Design 4
About Lesson

It’s a good practice to include test cases for normal scenarios, edge cases, and negative inputs whenever we write tests.

Good practice to include test cases - GeekySanjay

The qualities of a unit test –

Being fast and simple -> There are  3 A’s principles:

  • Arrange (data preparation)
  • Act (calling the code)
  • Assert (comparing expected and actual results)

In the example test, the first two lines involve data preparation (Arrange), followed by the action of calling the ‘cal.add’ function (Act), and finally, the assertion of expected and actual results (Assert).

Backend-LLd-4

Isolation -> in test cases means they should not depend on each other; each test should be independent. It’s better to pass static data that we can predict instead of relying on data from other tests

In the screenshot below, we have a class called Calculator, and we’ve written a corresponding test class, CalculatorTest, to test it.

In CalculatorTest, we’ve instantiated one Calculator object and written a test method t1. In this test, we set a to 10 and b to 20, and then call the add method on the calc instance. We expect the result to be 30, and this test passes.

However, in the second test method, we reuse the same calc object. We set a to 20 and b to 30, expecting the result to be 50. Unfortunately, this test fails because the result will be 50 + 30 from the previous test. To address this, we need to reset the result or use a different instance of Calculator in each test. This practice is called isolation.

Isolation

Repeadable -> Repeatability in testing means that running the same test multiple times should yield the same result consistently. For instance, if we have a test involving threading, and each run produces a different result due to concurrency issues, it indicates a problem with the test’s reliability.

Self checking -> Self-checking in testing means that tests should not require human interaction; they should run automatically

Self Checking

Test the behavior, not the implementation -> For example, when testing a sorting function, focus on verifying that the function returns a sorted result, rather than the specific sorting algorithm used internally, such as merge sort or bubble sort.

Test-the-behavio

© GeekySanjay