Conditional Test Logic
Definition:
Test methods need to be simple and execute all statements in the production method. Conditions within the test method will alter the behavior of the test and its expected output, and would lead to situations where the test fails to detect defects in the production method since test statements were not executed as a condition was not met. Furthermore, conditional code within a test method negatively impacts the ease of comprehension by developers.
Also Known As:
Indented Test Code, Guarded Test
Code Example:
@Test
public void testSpinner() {
for (Map.Entry entry : sourcesMap.entrySet()) {
String id = entry.getKey();
Object resultObject = resultsMap.get(id);
if (resultObject instanceof EventsModel) {
EventsModel result = (EventsModel) resultObject;
if (result.testSpinner.runTest) {
System.out.println("Testing " + id + " (testSpinner)");
//System.out.println(result);
AnswerObject answer = new AnswerObject(entry.getValue(), "", new CookieManager(), "");
EventsScraper scraper = new EventsScraper(RuntimeEnvironment.application, answer);
SpinnerAdapter spinnerAdapter = scraper.spinnerAdapter();
assertEquals(spinnerAdapter.getCount(), result.testSpinner.data.size());
for (int i = 0; i < spinnerAdapter.getCount(); i++) {
assertEquals(spinnerAdapter.getItem(i), result.testSpinner.data.get(i));
}
}
}
}
}
References:
Quality attributes
- Code Example
- Cause and Effect
- Frequency
- Refactoring
An Exploratory Study on the Refactoring of Unit Test Files in Android Applications
Automatic Identification of High-Impact Bug Report by Product and Test Code Quality
Detection of test smells with basic language analysis methods and its evaluation
Enhancing developers’ awareness on test suites’ quality with test smell summaries
Handling Test Smells in Python: Results from a Mixed-Method Study
How are test smells treated in the wild? A tale of two empirical studies
Improving Student Testing Practices through a Lightweight Checklist Intervention.
On the Distribution of “Simple Stupid Bugs” in Unit Test Files: An Exploratory Study
On the diffusion of test smells and their relationship with test code quality of Java projects
On the distribution of test smells in open source Android applications: an exploratory study
On the test smells detection: an empirical study on the jnose test accuracy
Refactoring Test Smells With JUnit 5: Why Should Developers Keep Up-to-Date?
Refactoring Test Smells: A Perspective from Open-Source Developers
Test code quality and its relation to issue handling performance
TestAXE: Automatically Refactoring Test Smells Using JUnit 5 Features
The secret life of test smells-an empirical study on test smell evolution and maintenance
Toward static test flakiness prediction: a feasibility study
Understanding practitioners’ strategies to handle test smells: a multi-method study
Unit Test Smells and Accuracy of Software Engineering Student Test Suites