The BEST Design Pattern for Maintainable Tests

The BEST Design Pattern for Maintainable Tests

Gui Ferreira

1 год назад

2,059 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@NotInventedHereShow
@NotInventedHereShow - 01.08.2023 23:03

I like it. As a minimum, I almost always create a static CreateSut() in the test class I use in my tests to instantiate the SUT. If there is ever a change to ctor parameters, I just have to change the CreateSut method.

If there are "arrange steps" that are reused between tests, I'll usually just have CreateSutWithXxx() static methods, where Xxx is some description of the state the SUT is placed into, e.g. if testing a shopping cart, it could be CreateSutWithSingleItemAdded(). Makes the arrange step easier to understand at a glance.

I reach for more general builder patterns mentioned in this video if tests for a single SUT is split between test classes.

Ответить
@lunat38
@lunat38 - 03.08.2023 12:56

That is an ideal use case for Builders, well done!
And thanks for sharing.

Ответить
@BenvdStouwe
@BenvdStouwe - 07.08.2023 09:51

After years of using the Builder Design Pattern to setup testdata I have found a better solutoin: C# records. I create a static method that instantiates a "default" instance of a record and with the "with" operator I setup further properties. This way I don't have to create Builder classes with all their properties and methods while I maintain the same functionality.

Ответить
@fandermill
@fandermill - 08.08.2023 01:46

Thank you for this great content, Gui!
Do you think it is ok to set private fields with Reflection in the Build method to get the object in the needed state for the test? Or do you prefer an internal constructor/factory method that can be called trough the InternalsVisibleTo option? Or is there any other option?

Ответить
@bmassioui
@bmassioui - 18.08.2023 00:53

I prefer to go with factory method with private constructor ...

Ответить
@Laggie74
@Laggie74 - 22.11.2023 18:49

I can see that the Directors might be needed in more complex situations. But instead of creating multiple Directors for each pizza type, wouldn't it be simpler to just add a fluent method "StartWith(PizzaBuilder.PizzaType.Plain)" that takes in a class specific enum? I know this would place the responsibility of the default configuration as part of the Builder, but it would make sense here to simplify code maintenance and readability. You can still create the directors later if you want that control somewhere else.

Thanks for all the great content.

Ответить
@antonmartyniuk
@antonmartyniuk - 05.04.2024 08:20

I really like using this approach in my own tests, actually

Ответить
@emiraksoy1520
@emiraksoy1520 - 29.05.2024 20:36

Awesome ! Definitely I will try this on unit tests , can we apply this integration tests ?

Ответить
@WhisperII
@WhisperII - 26.06.2024 12:55

pizza without cheese is not a pizza))

Ответить
@kitsurubami
@kitsurubami - 13.10.2024 18:14

Yay design patterns! I love your accent by the way. "Margherita Director" has such beautiful pronunciation. I will definitely be using the builder pattern to help reduce redundancy in my tests. Thank you!

Ответить