Code Smell 203 - Irrelevant Test Information
Tags: webdev, beginners, programming, python
Published: 2023-03-26 03:23:50

*Irrelevant data distract the reader's attention*

> TL;DR: Don't add unnecessary information to your assertions

# Problems

- Readability 

- Maintainability

# Solutions

1. Remove irrelevant data 

2. Leave only the needed assertions

# Context

Tests should be minimal and follow the SetUp/Exercise/Assert pattern

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/913c97719a02e75721ea53d8e4e674c7)
```python
def test_formula_1_race():
    # Setup
    racers = [
        {"name": "Lewis Hamilton", "team": "Mercedes", "starting_position": 1, "car_color": "Silver", "car_model": "W12"},
        {"name": "Max Verstappen", "team": "Red Bull", "starting_position": 2, "car_color": "Red Bull", "car_model": "RB16B"},
        {"name": "Sergio Perez", "team": "Red Bull", "starting_position": 3, "car_color": "Red Bull", "car_model": "RB16B"},
        {"name": "Lando Norris", "team": "McLaren", "starting_position": 4, "car_color": "Papaya Orange", "car_model": "MCL35M"},
        {"name": "Valtteri Bottas", "team": "Mercedes", "starting_position": 5, "car_color": "Silver", "car_model": "W12"},
    ]

    # Exercise
    winner = simulate_formula_1_race(racers)

    # Test
    assert winner == "Lewis Hamilton"
    
    # This is all irrelevant to winner asserting
    assert racers[0]["car_color"] == "Silver"
    assert racers[1]["car_color"] == "Red Bull"
    assert racers[2]["car_color"] == "Red Bull"
    assert racers[3]["car_color"] == "Papaya Orange"
    assert racers[4]["car_color"] == "Silver"
    assert racers[0]["car_model"] == "W12"
    assert racers[1]["car_model"] == "RB16B"
    assert racers[2]["car_model"] == "RB16B"
    assert racers[3]["car_model"] == "MCL35M"
    assert racers[4]["car_model"] == "W12"

```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/ce5da0101b5e9eb72af22c12d6338f6a)
```python
def test_formula_1_race():
    # Setup
    racers = [
        {"name": "Lewis Hamilton", "starting_position": 1},
        {"name": "Max Verstappen", "starting_position": 2},
        {"name": "Sergio Perez", "starting_position": 3},
        {"name": "Lando Norris", "starting_position": 4},
        {"name": "Valtteri Bottas" "starting_position": 5},
    ]

    # Exercise
    winner = simulate_formula_1_race(racers)

    # Test
    assert winner == "Lewis Hamilton"  
```

# Detection

[X] Semi-Automatic 

We can find some patterns in not needed assertions.

# Tags

- Testing

# Conclusion

Tests should be prose. Always focus on the reader. It might be you a couple of months from now.

# Relations

https://maximilianocontieri.com/code-smell-76-generic-assertions

# More Info

- [xUnit Test Patterns: Refactoring Test Code](https://amzn.to/40z6atJ)

# Disclaimer

Code Smells are my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [Evan Demicoli](https://unsplash.com/@evandemicoli) on [Unsplash](https://unsplash.com/photos/HGCqL-tRcac)
    
* * *

> Take reasonable steps to test, document, and otherwise draw attention to the assumptions made in every module and routine.

_Daniel Read_
 
https://maximilianocontieri.com/software-engineering-great-quotes

* * *

This article is part of the CodeSmell Series.

https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code