Questions tagged [unit-testing]
Unit testing is a method by which individual units of source code are tested to determine if they are fit for use.
1,810 questions
0
votes
2
answers
125
views
Is integration-testing all services the best approach?
I am building an API project, where I have a controller called C1, which calls service S1. Within this service, there are multiple method invocations to services S2and S3 and S4, as well as a call to ...
5
votes
5
answers
574
views
Is it good practice to check exception messages in unit tests? [duplicate]
Imagine I have a function like this (written in Java):
void sayHello(String firstName, String lastName) {
if (firstName == null) {
throw new IllegalArgumentException("first name is ...
0
votes
1
answer
84
views
Automated Testing classes with an injected DbContext [duplicate]
I've been working on an implementation of a service, and have found that there are a number of operations where I need to read from a database to provide a caller with certain data or objects.
In-line ...
1
vote
1
answer
252
views
OO design - Process that relies on the current state of objects
We have a CAD software extension, where you can draw products like walls, doors, windows, etc., and you can export the drawing as an order, which contains the parts and costs.
As part of this export, ...
0
votes
3
answers
205
views
In unit testing, what is the difference between the arrange step in the AAA-pattern and a fixture?
When writing unit tests, I always doubt whether I should put preconditions into the unit test itself (as the "arrange" step) or in a fixture. Is there a difference between the two? When ...
4
votes
3
answers
313
views
The applicability of functional core - imperative shell to a cli program which contains a wrapper around a binary
Not long ago while I was exploring for solutions to test without mocks I've found out about the functional core, imperative shell architecture. It sounds great, I also think that it would play nicely ...
2
votes
2
answers
163
views
Test a script reading from external source
I regularly write scripts and small tools that read data from external servers.
I am still not sure what is the most efficient and effective way to test them:
Probably the most effective way would be ...
4
votes
4
answers
247
views
Modeling superstate and substate combinations in DDD
I am designing an analytics application using domain driven design and test driven development.
I am having difficulty modeling the following requirement:
The application shows a workitem's state. ...
9
votes
10
answers
2k
views
Efficiency of improving testability versus adding unit tests
My team is going to add unit tests to old code without modifying it, to pave way for future design improvements. One of the short term goals is to also reduce the number of newly introduced bugs, ...
1
vote
5
answers
557
views
Code reusability/inheritance introduces pointless testing
Say I have a bunch of classes that imitate cars: SportsCar, Truck, and SUV. All of these classes share some public methods like start() and stop() which they inherit from an abstract class Car. While ...
2
votes
4
answers
200
views
Is it possible to do black box tests when I inject dependencies in the class?
This is my case.
I have a class in which I inject a service as dependency, an IService.
I want to test one method of this class. This method use one of the methods of the service.
Now I want to test ...
4
votes
3
answers
869
views
Clearing static state before testing each method
My SUT class depends on external static state (which generally should be avoided, I know).
How do I make sure my tests do not depend on their order of execution?
I mean other by introducing some reset(...
0
votes
2
answers
241
views
Is Spring Boot Unit Test Coverage with Integration tests only a bad practice?
I have recently come across a few codebases at work where the previous developers chose to reach the >80% coverage criteria by writing only integration tests with the @SpringBootTest annotation ...
13
votes
10
answers
4k
views
Does unit testing spot bugs that QA testing typically does not?
By QA testing I mean like say integration testing, system testing, regression testing, user acceptance testing and the like. If unit testing is skipped altogether would bugs that typically are spotted ...
0
votes
2
answers
232
views
Testing C# app backed by MSSQL database and Entity Framework - seeking advices for going with "test double" approach
I am new to C#, and Windows (coming from Python, Linux background). I need to add tests
to an existing C# codebase, which relies on a MSSQL database and Entity Framework.
In my old days when testing ...
3
votes
2
answers
233
views
Ensuring unit test data stays accurate
Say I have a method that expects data to be in some form to perform accurately. In the actual service/application flow, that data is formed upstream in other methods or services. How can I ensure the ...
2
votes
3
answers
461
views
How to unit test private method that is used by many public methods under a common API without duping the tests
This question is a more specific version of the one I posted last week.
Let's say I have a complex data structure that is posted into a bunch of different services.
The interface of all services looks ...
0
votes
6
answers
356
views
How to unit test private method that is used by several public methods without duping the tests
Lets say I have a class with two public methods and one private method.
The private method is used by both public methods and unit tests that are written against either of the public methods, could ...
0
votes
4
answers
233
views
Avoid Test duplication if a function created via TDD has an implementation detail that is later used in other functions
Lets say I have an API with two functions:
CreateNewMachine(CreateMachineCommand createMachine);
EditExistingMachine(EditMachineCommand editMachine);
The internal logic of both functions should be ...
3
votes
3
answers
220
views
How to unit test a function (Windows API) that launches processes?
I've been tasked to use the Windows API (CreateProcessA, ExitProcess) to write a function that'll create, launch, and terminate some in-house applications (app1.exe, app2.exe). The two apps are fire-...
1
vote
1
answer
176
views
NUnit testOf attribute usage
What are the use cases of TestOf? I'm new to NUnit testing and from what I have seen, most people don't use this attribute that much.
From my experience, TestOf helped me to quickly identify what ...
2
votes
1
answer
514
views
Unit testing a Web Worker in JavaScript/TypeScript - Best Practice
I got assigned with writing unit tests for a class that instantiate a Worker inside in it's constructor. The code looks simmilar to this,
class SomeClass {
private _worker: Worker;
constructor(...
5
votes
5
answers
714
views
How to test for performance regression?
I am working on a refactor on a certain package (I can give details if asked). The package involves a clever lazy evaluation of a sort of nested sequence of arithmetic operations. If the numerical ...
0
votes
1
answer
180
views
Feasibility of using different java version for different project with different compatibility properties
I have multiple Flutter Android app projects that have different compatibilities of Java.
Now, what should I do if I am developing more than one project simultaneously?
If the first one uses Java 17, ...
0
votes
2
answers
444
views
Origins of Unit Testing in hardware?
According to the Wikipedia entry for Unit Testing, it is defined as a technique for testing components of a system in strict isolation from each other, and it is described as having been expressly ...
18
votes
10
answers
8k
views
Would a middle ground between unit and integration tests be optimal
I've read many posts about unit tests only testing one object/class and mocking of objects should only be for direct dependencies of the object under tests. The only other option discussed for ...
12
votes
9
answers
8k
views
is it okay to mock a database when writing unit test?
I read an article about unit testing by Vladimir Khorikov, written in 2020. Here is The Article
https://vkhorikov.medium.com/dont-mock-your-database-it-s-an-implementation-detail-8f1b527c78be
It said ...
0
votes
1
answer
560
views
How to avoid too much mocking in unit tests in a database-heavy method?
I have a service method, acceptOrDenyJoinRequest, which follows a fairly complex flow (as depicted in this diagram):
In my unit tests, the implementation details of this method are heavily reflected. ...
1
vote
1
answer
603
views
Should I skip unit tests if integration tests cover the same scenarios?
I have a service method called acceptOrDenyJoinRequest that follows a logic similar to this flowchart (green boxes are ignored in code, and the light gray box calls an external service). According to ...
0
votes
3
answers
483
views
Reusing constants in tests vs "magic" values
I'm writing tests to check if validation rules are working for my CRUD app. I have constants defining stuff like max length of each String field. I'm unsure if within tests I should create test data ...
1
vote
3
answers
486
views
Concurrent, identical instances as an integration test
Over the years it's becoming clear to me what's an integration test and what's a unit-test.
I understand some people more or less settle on their definitions but to be brief I consider testing one ...
18
votes
11
answers
6k
views
Why is global state hard to test? Doesn't setting the global state at the beginning of each test solve the problem?
According to Why is Global State so Evil?, I know one of the disadvantages of "global state" is that it makes code "harder to test". I do not disagree with this, but what I don't ...
2
votes
5
answers
498
views
How to follow Outside-In TDD with Micro-services and Micro-frontends?
I was exploring TDD, specifically the Outside-In TDD pattern, where we need to write the acceptance (integration) test and then jump on to granular unit tests to implement the feature and make the ...
0
votes
4
answers
335
views
TDD and code reusability
Let's say that I've been iterating over my feature A with TDD. After several red-green-refactor cycles, I ended up with a nicely polished implementation with a part of the SUT encapsulated into some ...
4
votes
8
answers
3k
views
Should I write and commit unit tests for minor changes and bug fixes?
For example, if the change is "return users' full names instead of just last names", is it worth it to add a test for it? Would it make the test suit fragmented and confusing?
Context: My ...
0
votes
3
answers
289
views
.Net 8 XUnit: How to mock MySql in a CQRS API using integration tests?
This is a rewrite of my own
.Net 8 XUnit: Should my tests use real data or how to mock MySql with CQRS?
in a much more clear way:
The reason for rewriting my previous question is because in the ...
3
votes
2
answers
687
views
.Net 8 XUnit: Should my tests use real data or how to mock MySql with CQRS?
I'm creating tests for our .Net 8 API. We use MySql for data storage and CQRS pattern for queries.
In my personal opinion, I shouldn't use the real database for testing, because you will use an ...
2
votes
2
answers
425
views
In unit testing: How to abstract a dependency of subject under test?
Disclaimer: I am learning unit testing. I am also kind of beginner in object-oriented design.
Currently, I am involved in the development of an application to manage the finance of a humble food ...
2
votes
1
answer
784
views
Approach to software testing with docker
When discussing the testing approach, we had disagreements.
We develop software that we package into an image and distribute. We have two suggestions for testing:
Build a separate image with a test ...
2
votes
3
answers
270
views
How to decouple spagheti code for unit tests [duplicate]
A little background on the project: we as a company receive a spaghetti source code, and into that we add even more spaghetti code. So with that I want to say that
complete restructuring and ...
1
vote
3
answers
826
views
Creating Unit and Integration Tests with Database elements
This is something that I've heard a number of opinions and theories about, but I'm still torn on how to go forward.
For context, this particular issue deals with the following technologies, in case ...
3
votes
3
answers
1k
views
TDD when removing long lived feature
Suppose I had some Manager class that I need to change in regards to existing functionality by removing code.
The Manager always sends an initial message after a connection was established to do an ...
3
votes
1
answer
277
views
Unit Test Against In-Memory vs Real Database for SwiftData Applications
I am writing unit tests for my SwiftData application. Currently, I am using in-memory database, which get reset after every test. What benefits will I gain if I start using real database? My main ...
1
vote
1
answer
218
views
Benefits of resolving dependencies with IServiceCollection and IServiceProvider for Unit Tests instead of inheriting mocks from a base test class
Articles such as this point out some of the pitfalls of manually instantiating dependencies in UTs, while showing some of the benefits of doing it, instead, with the .NET's dependency container by use ...
-3
votes
1
answer
112
views
Testablilty of setup with builder pattern for configuration
We have built a library which can handle RESTful requests based on configured endpoints.
A fluent builder is being used to create endpoint definitions (configurations). These definitions are bound to ...
2
votes
2
answers
240
views
How do we call the approach to unit test through an additional layer of abstraction? [closed]
There‘s an approach in unit testing where, instead of just writing the technical details in every test method, you create a library of helper functions like
givenServiceFails (that acts on mock ...
4
votes
5
answers
414
views
Does testing the public surface of a class test the behavior of code or the way it is written?
A follow up question to How do unit tests facilitate refactoring without introducing regressions?.
I said that integration tests test the behavior of the code, while unit tests, being tied to ...
38
votes
12
answers
8k
views
How do unit tests facilitate refactoring without introducing regressions?
We all know the standard TDD cycle: I first write a unit test, the smallest unit test that fails; then I write some production code, again the smallest amount of production code that is sufficient to ...
0
votes
1
answer
114
views
Testing Boundaries on Publisher/Subscriber patterns
I work with a integration project based on Publisher/Subscriber pattern whose subscriber end feeds a staging database which stores data to be pushed to another application. The staging database has ...
0
votes
3
answers
471
views
Would you test this piece of configuration code? How do I determine which code is worth testing?
We have a piece of code that decorates an interface to transparently add retry logic.
Inversion of Control configuration
service.AddOurRestApiClient()
.AddResilienceHandler("Retry", ...