121
votes
What is the point of unit tests?
It's easy to write tests that have no value.
Especially if your employer mandates them. "You wanted tests. Here's your tests." If that's your criteria for tests then you're exactly right. ...
75
votes
Why assert for null on a object before asserting on some of its internals?
Does checking for null has any benefits in the example provided ?
The only benefit I see from doing this is a somewhat clearer message on why the test failed.
You have successfully answered your own ...
61
votes
What is the point of unit tests?
Sorry that this answer is so long, but your points are well constructed and require details and nuance to counter. If you're looking for a TL;DR, I think the last section is the most important one for ...
27
votes
Which comes first: CD/Trunk-based development or microservices?
Consensus is you want CI/CD first and anyway, independent of your application's language, design or architecture. Whether you deliver trunk-based or use feature branching is also independent of CI/CD.
...
26
votes
What is the point of unit tests?
You work on legacy applications. Have you ever run into some weird edge case where you have a function returning a weird value intentionally because a third party system depends on you returning a ...
22
votes
Why assert for null on a object before asserting on some of its internals?
Code should be unambiguous where possible
If a random NullPointer is thrown in a test, I'm starting to investigate if the program is at fault, or if the writer of the test did just miss the fact of a ...
20
votes
What is the point of unit tests?
and yet I'm unaware of any instance of a unit test ever failing for any reason other than because the developer changed the unit under test without changing the unit test
That's the point: You are ...
13
votes
What is the point of unit tests?
What is the point of unit tests?
To document the contract.
To ensure the code fulfills the contract.
To serve as an example of how to use a component.
To prevent regressions.
To discourage ...
12
votes
Which comes first: CD/Trunk-based development or microservices?
Trunk based development with continuous develivery and microservices are mostly orthogonal concepts. However, for a huge monolith, the delivery cycles which usually require a certain amount of testing ...
10
votes
What is the point of unit tests?
Alternative answer: Ground for refactoring
Aside from what was said in the previous answers, each project deprecates from the day it started. No matter how hard you try, unless there is no new ...
9
votes
How to write effective automated tests when building GPT4 powered software?
Your primary goal is surely not to test GPT-4, that is the job of it's vendor OpenAI. Your goal is to test the software you write. So for your automated tests, have a stable interface around GPT-4 and ...
9
votes
Is it okay to use Dependency injection only because of unit testing?
It is absolutely okay to use a programming method only to support proper testing. Test suites are just as important as business code - the fact that you don't ship them to the customer is not ...
8
votes
Why assert for null on a object before asserting on some of its internals?
I have written a lot of unit tests.
I want basic facts to be tested first. I wrote those tests first, and I ensured my code passed them first.
When I write more advanced tests, my goal is to test ...
8
votes
Accepted
How to initiate unit testing when you're the grunt?
You are working on project S,
which lacks CI/CD nor even the ability to see an NUnit green bar.
Presumably it exports at least one public function you can call.
Create project T, which calls one or ...
7
votes
Accepted
What is a good unit testing strategy against a chain of public method calls?
Test against the interface, not the implementation. So, you'll have tests for each of the methods: public_a, public_b, and public_c.
When you put your tester hat on, you don't know that public_b calls ...
7
votes
Accepted
Unit testing code which does not establish correlation between input and output
Tests are not limited to verifying application logic. If all your test does is verify calls to mocks, then you are verifying implementation. You are right to question the value of such tests for the ...
7
votes
What is the point of unit tests?
To be honest, I struggle with the whole concept. I'm now working on (for me) a very large and complex system with thousands of unit tests ... and yet I'm unaware of any instance of a unit test ever ...
7
votes
Accepted
TDD - What to do when adding a new function on a dependency causes many previous tests to fail?
Me and lot of others are heavily against using Mocks. We consider mocks as sign of bad design and testing practice. What you describe is one of the reasons why. Mocks couple test way too tightly with ...
7
votes
Accepted
Is it worth unit testing hardcoded values?
Worth is subjective
"Is it worth it" is generally not objectively answerable when talking about realistic testing expectations.
Generally speaking, we don't get as much time to write tests ...
7
votes
Accepted
Why are sealed classes not allowed to be mocked?
Mocking a class usually works by using reflection to construct a subclass which has methods and properties that do nothing (or whatever is required for the mock).
"Sealed" classes cannot be ...
6
votes
Why assert for null on a object before asserting on some of its internals?
I have argued with a few colleagues that it's pointless to assert that 'response' is not null if you are going to then assert on some of it's internals.
It might be "technically" pointless ...
6
votes
Method that returns an object is it adequate for TDD?
You're focusing on the wrong thing.
A SymbolPairDto and a couple of prices go in and and a DecisionResponse comes out. So long as you know how to build all of those, and how to compare one ...
6
votes
Accepted
Testing for exception thrown, unit or integration test?
Doesn’t matter.
I can throw an exception from a standard library by trying to open a file that doesn’t exist or by dividing an int by zero.
What makes it a unit test only has to do with how you ...
5
votes
How to write effective automated tests when building GPT4 powered software?
As you say, because GPT and similar technologies are opaque, it is impossible to analyse the logic to determine how it should be tested in a conventional way.
Typically when writing automated tests, ...
5
votes
Unit testing code which does not establish correlation between input and output
In most descriptions of unit testing as a methodology there's an idea of unit tests being as independent of implementation as possible. This is easy to understand and implement in cases when code does ...
5
votes
Should edge cases be part of one test, or each their own case?
You are testing different aspects of the system under test, so those should be different test cases. However, you can use other techniques to reduce duplication. For example, you could extract the ...
5
votes
How do I mock API responses with security in mind?
Rather than sprinkle such conditionals throughout your program, refactor the code so that all communication goes through a single, minimal communications specialist. Then replace that specialist with ...
4
votes
Unit testing 'opaque' classes / structs
I need to rely on the push function working correctly. This is my problem, since I can't (and don't want to) access the implementation details, my tests must assume that some of my methods work when ...
4
votes
Testing an assertion across all test cases
A module that operates using a language as powerful as database query languages certainly warrants extreme paranoia while testing. And it is far worse to under-cover than to over-cover functionality.
...
4
votes
Accepted
Where do unit tests stop and integration tests begin according to the classical school of unit testing?
Where do unit tests stop and integration tests begin
One place to look for the answer is in the testing literature.
Integration tests: Tests that explore the interaction and consistency of ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
unit-testing × 1755testing × 463
tdd × 345
c# × 186
integration-tests × 145
mocking × 135
java × 113
design × 87
python × 66
c++ × 59
object-oriented × 53
dependency-injection × 52
.net × 50
javascript × 48
programming-practices × 47
php × 45
refactoring × 42
continuous-integration × 42
design-patterns × 38
test-coverage × 36
bdd × 35
database × 28
integration-testing × 28
architecture × 27
code-quality × 24