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,755
questions
0
votes
1
answer
217
views
Why are sealed classes not allowed to be mocked?
Using Moq framework, it is not allowed to mock a class in C# which is sealed. Same goes for many other frameworks as well. But why is it not allowed?
0
votes
0
answers
50
views
Is using @Transactional together with @Test annotation on the same test method a good practice?
In my current Java project, there is such style of integration test:
@Test
@Transactional
public void testBusinessTransaction() throws Exception {
//...complicate set up code and execution code
...
1
vote
2
answers
173
views
Is it possible to prove mathematically that unit testing reduces test effort as compared to integration testing? [closed]
I took out the request for a paper, thank you for spending so much time on this.
hopefully this edit is better.
I use the word "complexity" meaning "cyclomatic complexity" so if ...
1
vote
2
answers
69
views
Test-doubling a file path in Java/Groovy
I am trying to write a unit test for a helper method in my ApiRequestHandler which handles APIGatewayProxyRequestEvent . This class uses an EFS file path, which is where it writes files to. I have ...
-1
votes
1
answer
100
views
Alternatives to using mock libraries such as Mockito in unit testing
Typically, when writing unit tests I tend to need to stub out collaborators and also mock some behavior in one or more of the collaborating objects. Say if I am testing a Service that is using a Dao, ...
2
votes
3
answers
249
views
How do you decompose big object for testing?
I have a package that provides an object with quite a lot of features owned by it.
Let us say the object is an HTTPServer, and when the user initializes it by providing config and a request handler ...
1
vote
2
answers
204
views
Best practices for unit testing when breaking down functions into smaller ones
Say we have a function of the form
def func(num: int) -> int:
num = num + 1
num = 2 * num
num = num**3
return num
and let us act like each line is a long computation so that we ...
2
votes
3
answers
275
views
Should Acceptance Criteria be mapped to unit tests?
My organisation is adapting a new SDLC process, and a part of this is to force engineers to map acceptance criteria (AC) to unit tests written for each user story in the pull request (PR).
To satisfy ...
0
votes
0
answers
48
views
How do you decide what is worth writing a unit test for [duplicate]
In an app I'm working in there's a service class(among others) which is used just to call methods on other classes. Recently I've had to work in that area and add another method that does what all the ...
1
vote
1
answer
97
views
How do I mock API responses with security in mind?
My team and I are beginning to mock our API responses in our iOS app so we don't have to worry about our backend being up when testing.
I have conditional compilation directives based on the ...
1
vote
4
answers
362
views
Is it worth unit testing hardcoded values? [closed]
I recently worked on a project where the team were writing lots of unit tests for hardcoded values, for example, unit tests that test the value of a string constant. The justification being that they ...
2
votes
2
answers
144
views
Where to put interface files for mocking aka what are best practices for organizing interfaces in a C#/.NET Core project with NSubstitute, Moq etc.?
Question
I'm working on a C#/.NET Core project, and I'm looking for guidance on organizing interfaces, especially when it comes to using NSubstitute, Moq or other libraries for mocking, because there ...
1
vote
2
answers
114
views
When writing automated tests for a website relying heavily on a template language, how do I gain confidence while maintaining fast feedback?
I'm working on a website that, for all intents and purposes, functions as a Choose Your Own Adventure book rendered as a single page app (SPA). Starting at the left, this is the user experience:
Each ...
1
vote
3
answers
167
views
Isolate or Redundant Test?
I have lots of code like the following. An "Entity" type that has some numerical properties. To be able to reuse the arithmetic I write the arithmetic functions against an interface. I use ...
2
votes
3
answers
215
views
Should edge cases be part of one test, or each their own case?
I am writing tests TDD-style for a cryptography package containing encryption and decryption methods. I test my methods with various kinds of inputs, including multiline strings, mixed case, calling ...
1
vote
3
answers
186
views
How do you test whether schema changes will break services?
I maintain a relational database of sorts, and occasionally a feature request requires a schema change (eg. add a new column, etc...).
However, a number of "services" consume this data. It's ...
0
votes
4
answers
401
views
Testing for exception thrown, unit or integration test?
In a test, asserting if a simple method throws an exception under a certain circumstance, is such a test considered a unit or integration test when the exception object thrown is from the standard ...
1
vote
3
answers
191
views
TDD - What to do when adding a new function on a dependency causes many previous tests to fail?
I was programming today and encountered something that just feels like I'm doing something wrong (maybe?). I've encountered this situation before, but I wanted to reach out and ask if there's a better ...
120
votes
19
answers
13k
views
What is the point of unit tests?
I've been a software developer for 20+ years, although it's been an unusual career. I've mostly worked on either legacy projects, or small standalone, non-public-facing apps and so only a couple of ...
2
votes
2
answers
216
views
Is it okay to use Dependency injection only because of unit testing?
I have a class that has dependencies that I know are not going to change.
class ConversationFinder
{
public function __construct(
protected Conversation $conversationDbFinder = new ...
2
votes
3
answers
311
views
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 ...
9
votes
5
answers
4k
views
Which comes first: CD/Trunk-based development or microservices?
My company* wants
to move from using long-running feature branching (~up to a few weeks) to continuous integration with trunk-based development, and
to break up our monolith into microservices.
We'...
-1
votes
3
answers
113
views
Code Coverage and Unit Tests nomenclature [closed]
About tests:
I have the following view on nomenclature:
Unit tests are the kind of testes where you have a function
ExtractBacon, where there is a function with an entry parameter Pig
and a return of ...
2
votes
2
answers
384
views
Does the "Pyramid of tests" contradict the idea of tests focusing on behaviour?
I am talking about these two testing strategies :
Have a pyramid of tests (with many more unit tests than high-level tests), because high level tests are harder to maintain and slower (see also : ...
3
votes
7
answers
343
views
How can unit tests be atomic in server-dependent CRUD apps?
Suppose that the following are true:
You believe that unit tests should be atomic. That is, tests should always test exactly one thing.
You have written a CRUD app in a general-purpose language such ...
0
votes
2
answers
216
views
What is a good unit testing strategy against a chain of public method calls?
say I have this code which is a chain of public methods, public_c calls public_b calls public_a
def public_a(...):
...
def public_b(...):
...
public_a(...)
def public_c(...):
...
...
0
votes
3
answers
208
views
Test driven reduction of technical debt
Suppose you have been put in charge of an already existing project. As you are starting to familiarize yourself with the repository, you notice a few technical debt issues (insufficient test coverage, ...
3
votes
3
answers
734
views
How to write effective automated tests when building GPT4 powered software?
Part of my current product uses GPT-4 with a prompt to extract information from plain text. As behaviour of large language models is inherently opaque and failure cases unpredictable, I was wondering ...
-1
votes
1
answer
58
views
Efficient way to write test cases depending on a Micro service
I'm very new to microservice architecture. In the Monolithic app structure, it was pretty straightforward to write test cases since everything was in one app. I have a situation where I manage a ...
7
votes
5
answers
677
views
How to initiate unit testing when you're the grunt?
I am the newest programmer on a very small team of 6 developers, 3 of which are offsite. By "newest" I mean both in years of experience (drastically) and familiarity with the code. I've been ...
2
votes
3
answers
239
views
Method that returns an object is it adequate for TDD?
Let's suppose I want to follow TDD (Test driven development) and I want to implement a class that is supposed to return a considerable object.
It starts to get really complex in my opinion and doesn't ...
19
votes
8
answers
6k
views
Why assert for null on a object before asserting on some of its internals?
Let's consider the following test.
[Fact]
public void MyTest()
{
// Arrange Code
var sut = new SystemWeTest();
// Act Code
var response = sut.Request();
// Assert
...
0
votes
1
answer
394
views
Should I use my View Model to Test my Views in SwiftUI?
I was reading a post about testing in SwiftUI and the author said that don't write unit tests for your View Models that tests the UI of your view. The example he gave was a simple counter app.
...
2
votes
2
answers
407
views
Where do unit tests stop and integration tests begin according to the classical school of unit testing?
There are two schools of thought on how unit tests should be written. The first is the Classical/Chicago school, which focuses on the isolation of unit tests, describes a unit as a class or set of ...
4
votes
2
answers
394
views
How to write maintainable unit tests using classical style of unit testing
When I am using the classical style of unit testing, how do I keep the number of test cases for an object that has many collaborators from growing too large? And how do I keep the setup of each test ...
0
votes
0
answers
65
views
Unit testing with dependency on Word COM objects: Wrappers & interfaces vs document as an embedded resource
We are developing Word Addins that interact with elements in Word documents.
To make it testable, my approach has been to use interfaces and wrappers for UI elements, and then provide mock alternative ...
0
votes
4
answers
359
views
Dependency injection in legacy code
I have a legacy project with many classes with high coupling, and I have a necessity to test some of them. So, I decided to introduce dependency injection principle but stumbled upon where to create ...
2
votes
3
answers
185
views
Unit Testing Internal Classes
Advice on unit testing is often focused on publicly exposed classes. What about cases where the only public class is simply there to instantiate internal classes? Should the internal classes be unit ...
0
votes
1
answer
83
views
Do we need to test an integration of units if each unit has been tested against collaborator (mocks) and interface contracts?
Given:
Let's say I have 3 objects A, B, C that form a cluster. Each unit (A,B,C) is independently tested with mocked collaborators. The "integration" is the call to A that calls out to B and ...
0
votes
2
answers
142
views
Testing an assertion across all test cases
I have recently developed a small module that performs queries against a database. At the end of each test that contains code performing modifying queries against the database, I added an assert that ...
0
votes
5
answers
604
views
Unit Testing - should / how should I write tests to cover new code that doesn't affect the interface of a method?
Having been trying to improve my unit tests, I'm trying to adhere to the principle of avoiding call verification. This is because it aligns with other principles I believe to be true:
We Should test ...
0
votes
2
answers
160
views
Unit testing 'opaque' classes / structs
I'm sorry, no good title.
I'm working on a C project right now and am writing tests. I usually do a 'struct method' style of programming. So I have opaque structs (no implementation revealed outside ...
2
votes
1
answer
208
views
How to avoid class-proliferation when creating a unit-testable system?
I've been working on a personal project for some time, and I believe that I'm at that point where, if I don't embrace unit tests (and ideally TDD) now, it may be too late, so I want to make the right ...
0
votes
1
answer
143
views
Path coverage and decomposition
Consider unit testing for a program written in procedural style. Each unit test tests a single function.
Consider the following example, in which I use C_i(x) for a condition depending on the argument ...
0
votes
1
answer
485
views
What should you unit test in (external) service driven architecture?
My system consists of 4 seperate layers.
API layer
Application layer
Infrastructure layer
Core layer
System is not Domain Driven. It contains no entities with domain logic. All models are anemic. ...
0
votes
1
answer
801
views
When unit-testing should common methods put in static class or in base class?
I have a few methods that are used from different class tests. Also, some other common methods are used for preparing the test data.
What is the common practice to handle this in tests?
Should I ...
-1
votes
1
answer
122
views
Software development in Python: unittests and assertions
I recently finished developing a piece of software in python where learning models and data processing procedures are contained within different class objects that succeed to each others (modules). As ...
0
votes
1
answer
169
views
How to create useful tests for fixed byte values
I am working on an android application with Bluetooth iot devices. Theses devices sends broadcasts periodically in the form of 30ish bytes containing various information.
Some of the bytes have ...
0
votes
3
answers
177
views
Tests of 1-Dependency VS Tests of 0-Dependency
How many dependencies does a unit test have (that are not mocked)?
My understanding is that a true "unit test" has zero dependencies, with all of them mocked (or none in the first place; ...
2
votes
1
answer
192
views
Test coverage for various code logic permutations... Metadata/Artifacts in algorithm results to describe which case?
I'm sorry if the title is confusing, I don't know if what I am describing has a proper name so let me describe...
I have an algorithm which contains quite a bit of nested if/else if/else logic for ...