Chapter 8 - Testing Flashcards
(50 cards)
test pyramid
pyramid partitions the tests according to their granularity.
Unit tests
check small parts of the code, usually a single class.
They form the base of the pyramid, meaning most tests fall into this category. Unit tests are simple, easier to implement, and fast to run.
integration tests or service tests
verify a system’s functionality or transaction
These tests involve multiple classes from different packages and may include external components like databases. They require more time to implement and are slower to run.
end-to-end tests, also referred to as user interface tests or system tests
simulate a user session on the system as authentically as possible.
They are more complex, time-consuming, and fewer in number. End-to-end tests also tend to be brittle, meaning minor alterations in the user interface might require changes in these tests.
Automated test percentage breakdown
A generic recommendation suggests that automated tests should be implemented in the following proportion: 70% as unit tests; 20% as integration tests; and 10% as end-to-end tests
defect (bug)
when a piece of code does not comply with its specification
failure
If defective code is executed and causes the program to produce an incorrect result or behavior
fixture
create the test context
This involves instantiating the objects we intend to test and, if necessary, initializing them
The program state verified by one or more test methods, including data, objects, etc. In the context of unit testing, the function of a fixture is to fix the state, i.e., set up the data and objects to be verified by the test.
Test Method
A method that implements a test and is annotated with the @Test annotation
Test Case
In JUnit, the term refers to a class containing test methods. The name originates from the first versions of JUnit, where the test methods were implemented in classes that inherited from a TestCase class.
Test Suite
A set of test cases typically executed together by the unit testing framework, which in our case is JUnit.
System Under Test (SUT)
The system being tested. It’s a generic term, also used in other types of tests, not limited to unit tests. The term production code is also sometimes used to refer to the SUT.
Test-Driven Development
Development style: you can write the tests first, before any production code. Initially, these tests will fail. Thus, you start with code that only compiles but whose tests fail. Then you implement the production code and test again. At this point, the tests should pass.
regressions
occurs when a modification in one part of the code—whether to fix a bug, implement a new feature, or perform a refactoring—inadvertently introduces a bug in another part.
FIRST
Fast
Independent
Repeatable
Self-checking
Timely
Fast
Developers must run unit tests frequently to receive feedback about bugs and regressions. Therefore, it’s important that these tests execute quickly, ideally in milliseconds.
Independent
The order of execution of unit tests should not matter. For any two tests T1 and T2, running T1 followed by T2 must produce the same result as running T2 and then T1. Indeed, T1 and T2 could also be executed concurrently. For the tests to be independent, T1 should not change any part of the global state that is later used by T2, and vice versa.
Repeatable
Unit tests should always provide the same result. This means that if a test T is called n times, the result should be the same in all n executions. Therefore, T should either pass in every execution or fail in every execution.
Self-checking
The result of unit tests should be easily verifiable. Developers should not have to open and analyze an output file or manually provide input data to interpret the test result. Instead, the results should be displayed in the IDE
Timely
Tests should be written as early as possible, ideally even before the code that needs to be tested.
Flaky Tests or Erratic Tests
Tests with non-deterministic results
Concurrency is one of the main causes of flaky behavior
Test Smells
represent sub-optimal implementation decisions in test code, which, in principle, should be avoided
Includes:
Obscure Test
Tests with Conditional Logic
Code Duplication
Obscure Test
a long, complex, and difficult-to-understand test
Tests with Conditional Logic
include code that may not be executed.