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.

Filter by
Sorted by
Tagged with
0 votes
3 answers
119 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
77 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 ...
  • 163
3 votes
1 answer
266 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 ...
2 votes
2 answers
165 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
191 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 ...
  • 41
8 votes
9 answers
698 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
144 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 ...
  • 4,873
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
437 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
125 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 = ...
  • 999
-1 votes
1 answer
85 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....
1 vote
2 answers
155 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
77 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-...
  • 945
0 votes
4 answers
277 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() { ...
  • 347
0 votes
5 answers
314 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 ...
  • 125
-3 votes
3 answers
205 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
2k 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
245 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(); ... } } @...
  • 181
2 votes
2 answers
129 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 ...
  • 131
0 votes
1 answer
63 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
284 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 ...
  • 133
0 votes
2 answers
194 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
198 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 ...
  • 3
-1 votes
2 answers
136 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
157 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 "...
  • 147
1 vote
4 answers
332 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. ...
  • 302
3 votes
1 answer
219 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....
  • 195
2 votes
4 answers
380 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
234 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 ...
  • 131
-3 votes
1 answer
82 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 ...
  • 131
5 votes
2 answers
526 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 ...
  • 59
9 votes
5 answers
843 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 ...
  • 705
2 votes
4 answers
282 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 ...
  • 37
0 votes
3 answers
90 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
134 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 ...
0 votes
1 answer
254 views

How can I unit test state machines in C

In an 8bit mcu, I wrote a piece of code of a state machine using a function pointer static void (*state)(void);. Inside the same compilation (let's say sm.c) unit I implement all the available states. ...
  • 203
2 votes
3 answers
321 views

Still don't understand when to mock and when not to

I've been trying to understand when to mock and when not to mock, however I'm not able to come up with a consistent guideline and I'm hoping to get some input on the subject. Let's look at the ...
  • 155
1 vote
1 answer
238 views

How to test around a giant static class?

I am dealing with an old .net code base which has a PrivilegeChecker static class with hundreds of static methods, each of which takes in some user id and some other params, and then fetches some info ...
  • 196
2 votes
2 answers
95 views

Replacing service implementation based on environment variables

I have previously asked a question on this forum linked here. In short, I wanted to not call 3rd party APIs when developing locally. Based on the wonderful answers received I have opted for the ...
  • 161
0 votes
0 answers
68 views

What is the correct approach to test CDI classes in service layer?

My app has a service layer which is composed by CDI @ApplicationScoped beans. This is a class extracted from my project: @ApplicationScoped public class PostService { @Inject private ...
  • 309
0 votes
1 answer
129 views

Examples of software testing? [closed]

I'm learning about testing. So far, I kind of get the gist of it but it's still a bit confusing. I thought about an app like Photoshop. Unit testing would be the developer testing the 'cancel' button. ...
0 votes
2 answers
138 views

What are the reasons not to use random values in unittests? [duplicate]

I've read the questions regarding the use of random values in unit-tests and, well, I still don't quite understand what the argument against random values is. I'm trying to understand because I've had ...
  • 319
2 votes
1 answer
171 views

How to unit test code which is run asynchronous?

I have a bean which executes a method doStuff asynchronous, which of I am uncertain what is the best way to test the logic of that method doStuff. public class MyBean { private final ...
  • 399
-1 votes
2 answers
108 views

Unit testing inside the main project [closed]

Usually, unit tests are built in a separate project/executable. Ideally, components under tests should have a small number of mockable dependencies, allowing us to do this, among other benefits. But ...
-2 votes
2 answers
437 views

Best practices around writing testable extension methods

I’m currently working in C# and I’d like to write and extension method against a type. There is a small amount of repeated logic between classes surrounding JSON deserialization, using the ...
  • 1
34 votes
3 answers
5k views

What is the point of repeatedly executing the same test?

I have recently learned about the not-well-known and not-widely-used annotation @RepeatedTest that, as the name implies, repeats the very same test n-times. Baeldung provides a short guide to this ...
1 vote
2 answers
404 views

Unit testing of classes with functions as parameters in C++

Let's say I have a function in a class with the following signature: int fun(int x, int y,std::function<int(int, int)> funArg) The output depends on the operations done in funArg. My question ...
0 votes
1 answer
282 views

How to test a function with several conditional nested side effects

In Python, consider a function like the following: def main(*args): value1 = pure_function1(*args) if condition(value1): value = side_effect1(value1) if value: ...
  • 109

1
2 3 4 5
35