# Rules of painless unit tests

DevFeed: [Rules of painless unit tests](<https://devfeed.tech/articles/rules-of-painless-unit-tests-26136.md>)

Original publisher: [Read original article](<https://vadzimv.dev/2021/09/30/painless-unit-tests.html>)

Author: Vadzimv Dev Blog

Published: 2021-09-30T07:00:00Z

Content type: article

Language: en

Sources: [Vadzimv Dev](<https://devfeed.tech/sources/vadzimv-dev-blog.md>)

Topics: [Software Testing](<https://devfeed.tech/topics/software-testing.md>), [Unit testing](<https://devfeed.tech/topics/unit-testing.md>)

Tags: [api](<https://devfeed.tech/tags/api.md>), [bugs](<https://devfeed.tech/tags/bugs.md>), [code](<https://devfeed.tech/tags/code.md>), [communication](<https://devfeed.tech/tags/communication.md>), [complex](<https://devfeed.tech/tags/complex.md>), [dependencies](<https://devfeed.tech/tags/dependencies.md>), [details](<https://devfeed.tech/tags/details.md>), [email](<https://devfeed.tech/tags/email.md>), [external](<https://devfeed.tech/tags/external.md>), [factory](<https://devfeed.tech/tags/factory.md>), [fake](<https://devfeed.tech/tags/fake.md>), [filesystem](<https://devfeed.tech/tags/filesystem.md>), [multithreading](<https://devfeed.tech/tags/multithreading.md>), [network](<https://devfeed.tech/tags/network.md>), [random](<https://devfeed.tech/tags/random.md>), [red](<https://devfeed.tech/tags/red.md>), [rules](<https://devfeed.tech/tags/rules.md>), [tests](<https://devfeed.tech/tags/tests.md>), [unit-tests](<https://devfeed.tech/tags/unit-tests.md>), [useful](<https://devfeed.tech/tags/useful.md>)

## AI overview

The author's core takeaway is to make unit tests specific to observable behavior so refactoring does not break tests when functionality still works. Use the SUT's public API as clients do, hardcode expected results, and keep test logic simple. Isolate tests from networks, filesystems, time, randomness, concurrency, sleeps, and delays with fakes, stubs, or mocks. Use fakes or stubs to provide dependency results; reserve mocks for verifying important external interactions. Check only behavior that matters, since internal calls and unrelated fields can change without affecting the feature. The excerpt also begins a rule favoring factory methods over constructors for test objects, especially DTOs, but cuts off before explaining how.

## Source excerpt

Rules that make unit tests useful.