New answers tagged unit-testing
5
votes
Accepted
How to avoid too much mocking in unit tests in a database-heavy method?
How can I avoid excessive mocking and reduce the tight coupling between the tests and the implementation?
Don't mock things. Or only mock things you cannot otherwise test.
If your service requires a ...
5
votes
Accepted
Should I skip unit tests if integration tests cover the same scenarios?
What is an integration test?
and integration tests should be written for pieces of code that interact with external systems, like databases.
Firstly, while this is not the main point of the answer I ...
-1
votes
Reusing constants in tests vs "magic" values
Using constants in test rather than magic values improves readability, maintainability, consistency, and overall code quality. its makes the intent behind the code clearer and ensure that changes can ...
2
votes
Reusing constants in tests vs "magic" values
I would really question the value of these "is the correct error thrown in X circumstance" tests.
Presumably you are not hand coding the error code and message for every error. You have some ...
4
votes
Accepted
Reusing constants in tests vs "magic" values
First, it is never a good idea to distribute knowledge about the correct value for something like NAME_LENGTH_LIMIT over too many places in the code. Ideally, there should be a single source of truth. ...
2
votes
Accepted
Is testing multiple, concurrently running instances of the same class an integration test?
Already established in another answer, the "unit" in unit test does not exclusively refer to a single class. Sometimes a handful of classes work together as a unit, at which point they can ...
-1
votes
Is testing multiple, concurrently running instances of the same class an integration test?
The outer limit for a unit test is IO. No DB, no file system, no networking. So it depends on where these events are going. If you're sending them to some event server this isn't a unit test. If you'...
5
votes
Is testing multiple, concurrently running instances of the same class an integration test?
The "unit" in "unit testing" isn't necessarily a class. As Wikipedia correctly states:
Unit is defined as a single behaviour exhibited by the system under test (SUT), usually ...
1
vote
Why is global state hard to test? Doesn't setting the global state at the beginning of each test solve the problem?
Why is global state hard to test?
Global state is a subtle way of coupling every function that uses it. Setting the state before each test case is all fine and well, but guess what's not happening in ...
2
votes
Why is global state hard to test? Doesn't setting the global state at the beginning of each test solve the problem?
Trusting a global is like eating off the floor.
why would it be "harder to test"?
Because a global is known, globally.
doesn't "setting the global state at the beginning of each test&...
6
votes
Why is global state hard to test? Doesn't setting the global state at the beginning of each test solve the problem?
One thing i don't see anyone else bringing up here is the set of unique bugs that global state makes more likely. Especially raceconditions become a lot more likely to occur in an application with ...
4
votes
Why is global state hard to test? Doesn't setting the global state at the beginning of each test solve the problem?
I think you're conflating two things, basically the words "any" and "all".
Does there exist global state that can be tested in the way you describe?
Yes. In a suitable language you ...
1
vote
Why is global state hard to test? Doesn't setting the global state at the beginning of each test solve the problem?
There are already good answers, but one additional consideration for some cases:
Global state management (reset and access) likely will require global initialization, i.e. to first reset the global ...
16
votes
Why is global state hard to test? Doesn't setting the global state at the beginning of each test solve the problem?
My question is, why would the answers assume that?
Because it is the default unless you change it.
If I need to test a function with a global state, isn't just "restoring the global state at ...
1
vote
Why is global state hard to test? Doesn't setting the global state at the beginning of each test solve the problem?
I don't think there is one universal understanding of what "global state" even is.
I suppose it is commonly used as a synonym for "global variables" - that is, a facility offered ...
60
votes
Why is global state hard to test? Doesn't setting the global state at the beginning of each test solve the problem?
How do you know where all the global state is in your application?
When you have a function which does not use global state, all the information required for test state is in the parameters. When a ...
35
votes
Why is global state hard to test? Doesn't setting the global state at the beginning of each test solve the problem?
If you reset the global state before each test, each test will execute in a known global state and will, therefore, be consistent. Trouble is, that's not how it will be executed in the real ...
7
votes
Why is global state hard to test? Doesn't setting the global state at the beginning of each test solve the problem?
In an application, you usually start with global state initialised from settings and preferences, and then you add more state as the application is running.
When you call a method in real code, it ...
-2
votes
How to follow Outside-In TDD with Micro-services and Micro-frontends?
As per all other answers on this post, here is the summarized version of how we can implement the Outside-In TDD for micro-services, where one micro-service is dependent on another to complete any ...
2
votes
How to follow Outside-In TDD with Micro-services and Micro-frontends?
Your top level acceptance test just needs a stable API to act through. It doesn't matter how many micro-services are needed to support it. What matters is, as you flesh out the micro-services, you ...
2
votes
How to follow Outside-In TDD with Micro-services and Micro-frontends?
Your case will allow it to write outside-in tests on three different levels (at minimum):
outside tests for the "system as a whole" - which are real integration tests, showing the different ...
0
votes
How to follow Outside-In TDD with Micro-services and Micro-frontends?
The main point of microservices is that they are separate. A microservice has some API, and there are consumers of that API. If another service needs some new API they make a feature request, you ...
4
votes
How to follow Outside-In TDD with Micro-services and Micro-frontends?
There's not just one feature
where a single feature can span across several Micro-services?
For all intents and purposes, this isn't one feature spanning across several microservices, this is a ...
Top 50 recent answers are included
Related Tags
unit-testing × 1786testing × 474
tdd × 351
c# × 188
integration-tests × 152
mocking × 138
java × 114
design × 88
python × 66
c++ × 60
.net × 54
dependency-injection × 54
object-oriented × 53
javascript × 49
programming-practices × 47
php × 46
refactoring × 45
continuous-integration × 42
design-patterns × 39
test-coverage × 36
bdd × 35
database × 28
integration-testing × 28
architecture × 27
code-quality × 24