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,720
questions
0
votes
0
answers
25
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
130
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
158
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
68
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 ...
1
vote
2
answers
110
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 ...
1
vote
5
answers
210
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
69
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
141
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
55
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
110
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
198
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
119
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
117
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
162
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
105
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 ...
3
votes
1
answer
289
views
Why does this activity diagram not have the same amount of paths as the cyclomatic complexity number?
For the example shown below, in which I added the True and False values over the original document (so if something is wrong there, it's probably my fault), I understand there should be seven states ...
3
votes
2
answers
179
views
Abstracting calls to common library methods in C#
I‘m working on making a legacy code base more testable and made good progress with (constructor)-injecting dependencies to classes.
I noticed something in all classes that access the file system: they ...
4
votes
5
answers
355
views
When mocking a class in a unit test, how should I handle dependency classes that have multiple similar get methods?
Let's say there's a class that processes text, and it gets that text from another class as a buffer. If this buffer class has multiple get methods, like readLine(), readChar(), readCharCode(), how ...
8
votes
10
answers
804
views
Can you have too much unit test coverage?
Google tells me that the ideal unit test coverage is 70 - 80%
(although Google's source for that doesn't look especially credible). This old post suggests that in fact it depends on what gives you ...
0
votes
1
answer
324
views
Mocking / stubbing C# record types in a unit test
I have a series of record types in my C# project that I use with JSON.Net to deserialize json data.
public record RadarrMetadata(
IReadOnlyCollection<string> CustomFormats
);
public record ...
0
votes
2
answers
137
views
Writing tests for API that results depends on table with millions of rows
I think my problem is very common but I couldn't phrase it correctly in the title.
I have a Django API that returns some information about companies (like address, partners, situation, etc) given its ...
8
votes
3
answers
475
views
How can you write unit tests for an opaque/nondeterministic algorithm like Monte Carlo tree search?
I've written an implementation of the UCT1 Monte Carlo Tree Search algorithm for selecting moves in a two-player game. In the future, I'd like to expand this implementation to use more advanced tree ...
0
votes
1
answer
150
views
Should mocks used in unit tests represent entireties or subsets?
I was working on a unit test that has a mock object looking something like:
public class TestMock
{
static {
Doodad a = new Doodad(0);
Doodad b = new Doodad(1);
Doodad c = ...
-1
votes
1
answer
90
views
Do you test your CI/CD scripts? [closed]
Is it common to write test for CI/CD scripts?
Background: Some CI/CD jobs/scripts have become rather complex over the years (new features, edge cases) and I believe it would help rewriting them with e....
2
votes
2
answers
162
views
C# - Class creating instances of other classes requiring different services
Context
I have a service class whose sole purpose is to interact with a specific API, let's say the Automotive API. The API mostly works with generic AutomativeRecord which basically represents a ...
0
votes
0
answers
78
views
Unit test coverage and abundant glue code [duplicate]
How do I solve this riddle of contradicting "good practices" to properly cover my app with unit-tests?
These are principles I found about writing unit-tests:
Pyramid of testing says unit-...
0
votes
4
answers
306
views
Should i write tests if it takes more time than testing manually?
Im making a website and trying to learn Test Driven Development (TDD)
I'm doing one of CS50 projects, were we need to make a website to trade stocks. I decided to use this oportunity to learn TDD. I ...
9
votes
8
answers
6k
views
Is it bad practice to compare string representation on an object instead of its properties during unit tests?
Given a class as follows:
class IntList {
public:
IntList(std::vector<int> list) : m_list(list) {}
std::vector<int> list() { return m_list; }
std::string toString() {
...
0
votes
5
answers
317
views
What arguments to use when explaining to a team that one does not write unit tests in a different language?
I'm helping a software team to embrace a transition to DevOps and I realized that they struggled a lot with testing. The one point that troubles me the most is that they absolutely want to write unit ...
-3
votes
3
answers
207
views
Where did the guideline of writing automated unit tests for public methods (only) originate? [closed]
The question is simple: there is no shortage of opinions that state automated unit tests are to be written only for public methods.
What is the authoritative source and argument behind this best ...
9
votes
3
answers
3k
views
Why is having bloated interfaces an anti pattern?
[I'm using the term interface here with C#'s interface in mind. I won't tag the question with a C# tag because it really isn't a C# question.]
In my work we do unit and integration tests but we don't ...
0
votes
1
answer
396
views
Mock a bean with 10 methods when I only use one?
I face some situations similar to the following simplified one:
@Component class ServiceOne {
@Autowired ServiceTwo two;
void act() {
...
two.a();
...
}
}
@...
2
votes
2
answers
154
views
What is a good way to call a unit-tested function provided by a library/package?
Consider the function foo provided by package X in Python. I want to test the different functionalities of X.foo, and then use X.foo in my code. To make sure that I am using X.foo as it was tested, I ...
0
votes
1
answer
98
views
How to properly isolate tests for dataframes with grouping?
I have the following problem:
I would like to test complex business logic for each test case completely separately, i.e. all tests should run in parallel. I don't want the test for customer #43 to ...
3
votes
3
answers
352
views
In testing is making asserts in cleanup bad?
As a learning excercise, I decided to take a hand on Test Driven Development.
Now I decided that there is a test I want to make; check if database connection doesn't leave any unsaved changes.
The two ...
0
votes
2
answers
198
views
Is it worth testing simple details? [closed]
In front-end projects(made in frameworks like Angular or React), when we correctly encapsulate complex functionalities, the components that really have some relation to the business logic normally ...
0
votes
1
answer
311
views
Unit testing parts that use a Value Object that is being created by a Factory
Here is the conundrum,
I have a fairly complex Value Object and I don't want to expose it's internals. It should not be an Entity since there is no need for attaching an identity to it. According to ...
-1
votes
2
answers
190
views
Assert same and equals in unit test
I have a function to be tested
fn doNothing(Student student) {
//do some other operations here. but student is unmodified
return student;
}
And my unit test is
var student = new Student("...
12
votes
3
answers
2k
views
Should I choose repeated code in unit test or test logic? Can I avoid both?
When writing unit tests, I feel that there is a trade-off between code repetition and test logic.
Example of my current (likely flawed) approach:
To test this function (overly simple function for ...
0
votes
1
answer
206
views
Unit tests and mocking when functions arguments have changed?
I'm learning how to write unit tests on a project I'm doing with my friends. One thing we tried was to mock the return values of function calls if the functions are declared in a different "...
1
vote
4
answers
426
views
Is setting up a basic happy path for the unit tests in a single place a good idea?
I've come in to a project where it's a best practice for an Init method for all tests in a class to contain mocked happy path functionality. This should allow most methods to pass without exceptions. ...
3
votes
1
answer
267
views
Should I unit test "trivial" functions that are thing wrappers around the Pandas API?
I'm working on a codebase with very little testing. The code is 90% an ETL pipeline with functions like
def sort_data(df: pd.DataFrame, column: str = 'date') -> pd.DataFrame:
return df....
2
votes
4
answers
382
views
How can unit testing be employed to discourage developers from changing structurally-critical code?
Imagine a system for loan applications where loans can be in one of three states: PENDING, APPROVED, REJECTED.
Over time, people create code, UIs, internal reports, etc. that assume there are only 3 ...
2
votes
1
answer
323
views
Should integration tests of a repository pattern use low-level ORM calls
Problem summary:
In an application with wrapper methods over SQLAlchemy add() and query() methods, can integration tests that use the add() method wrapper use the query() method wrapper to validate ...
-2
votes
1
answer
87
views
Proper Way to Start Unit Testing a Web API [closed]
I need to start testing a Web API, for now I'm going to start with unit tests and I can't figure out the right way to do it. Here is what I have in mind doing:
The API consists on a list of endpoints ...
5
votes
2
answers
554
views
Should you write unit tests for templates and frameworks, such as in .NET?
How much (if any) unit tests should be written when you start a new templated project? In particular, let's take as an example a typical .net framework template for asp.net mvc or other similare ones ...
9
votes
5
answers
934
views
Today I wrote "tests" for the testing code. Was it the right thing? Is it a smell?
My application has big integration with the database. Classes that use the database are very crucial for the system, so I write small unit tests for classes that I call Repository. The reason behind ...
2
votes
4
answers
356
views
Is it ok to test an external function?
I have a utility function called connectToMongoDB() which connects to MongoDB. Basically, it creates a client for MongoDB and returns that.
I'm making a library and I'm thinking of unit testing the ...
0
votes
3
answers
94
views
Should we test private data (static objects in this case) to make sure it maintains its structure?
I had a discussion at work about whether to unit test a private static object we're using as data for a public component.
const data = {
45: { name: 'John' },
2: { name: 'Patricia' },
27: { name: '...
-1
votes
1
answer
197
views
How to write tests for a class that talks to a server without revealing connection implementation
I'm writing a class that acts as the interface to a server. The interface exposes a way to send messages to the server and pass messages back to the client through a callback. Implementations should ...