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
-1 votes
0 answers
36 views

How to correctly test a JSON parser with multiple methods

I wrote a parser that parse XML files of some format (EDIFACT) using PHP, to do this, I converted the XML to JSON then to php array (a Laravel collection more precisely) and then I started parsing ...
user avatar
0 votes
1 answer
111 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(); ... } } @...
user avatar
  • 181
1 vote
2 answers
118 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 ...
user avatar
  • 121
0 votes
1 answer
42 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 ...
user avatar
3 votes
3 answers
229 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 ...
user avatar
  • 133
0 votes
2 answers
177 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 ...
user avatar
0 votes
1 answer
135 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 ...
user avatar
  • 3
-1 votes
2 answers
117 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("...
user avatar
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 ...
user avatar
0 votes
1 answer
134 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 "...
user avatar
  • 147
1 vote
4 answers
271 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. ...
user avatar
  • 302
3 votes
1 answer
187 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....
user avatar
  • 195
2 votes
4 answers
375 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 ...
user avatar
2 votes
1 answer
94 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 ...
user avatar
  • 131
-3 votes
1 answer
76 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 ...
user avatar
  • 131
5 votes
2 answers
501 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 ...
user avatar
  • 59
9 votes
5 answers
739 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 ...
user avatar
  • 695
2 votes
4 answers
238 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 ...
user avatar
  • 39
0 votes
3 answers
85 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: '...
user avatar
-1 votes
1 answer
106 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 ...
user avatar
0 votes
1 answer
191 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. ...
user avatar
  • 203
2 votes
3 answers
289 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 ...
user avatar
  • 155
1 vote
1 answer
143 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 ...
user avatar
  • 196
2 votes
2 answers
71 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 ...
user avatar
  • 151
0 votes
0 answers
60 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 ...
user avatar
  • 299
0 votes
1 answer
125 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. ...
user avatar
0 votes
2 answers
111 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 ...
user avatar
  • 319
2 votes
1 answer
168 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 ...
user avatar
  • 379
-1 votes
2 answers
105 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 ...
user avatar
-2 votes
2 answers
146 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 ...
user avatar
  • 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 ...
user avatar
1 vote
2 answers
254 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 ...
user avatar
0 votes
1 answer
124 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: ...
user avatar
  • 109
0 votes
1 answer
87 views

Refactoring business logic out to private methods and testing

I am currently working on a project where there is a class ContactMonitoringJob a Quartz IJob class​ which in brief keeps pulling from a database and checks if any new changes have been introduced ...
user avatar
  • 59
0 votes
0 answers
48 views

Logic in lambda functions together with generic classes, unit-testing

I have a question regarding the use of lambdas in factories, together with generic/templated classes. It seems like it is possible to get carried away with lambdas and generic classes, to have too ...
user avatar
0 votes
3 answers
243 views

Should I unit test functions internally used by API I expose?

I'm writing a CRUD app in Python that exposes web API. At first I wrote functions for communicating with DB and wrote tests for these functions. def crud(): # do something with db def test_crud(): ...
user avatar
6 votes
4 answers
498 views

How to do unit testing with complex business logic with lots of different code paths?

This is an example of a typical function from a project I used to work on. I have troubling grasping unit testing because if I want to test this function properly, I would have to write about 50-100 ...
user avatar
  • 185
1 vote
1 answer
109 views

Unit testing, testing internals

I have a class, which uses a timer, to do some time-dependent things. Now, I do create the timer inside the class: class MyClass { public: Myclass(arg1, arg2, ...) :m_timer(new Timer(delay)) ... ...
user avatar
1 vote
1 answer
194 views

Should I check floating point values in a unit test?

We have unit tests that are running some underlying model. We provide it with some test input, and receive some outputs + floating point scores. What's a good practice from a unit-testing standpoint? ...
user avatar
  • 155
1 vote
0 answers
86 views

How to improve maintainability and testability of a service by refactoring it into a functional style

A customer of ours has a rather large (500+ LOC) service as central part of the project. Written in C# and consisting of rather clunky large imperative style functions, I was asked to improve the test ...
user avatar
0 votes
1 answer
50 views

The correct relationship between unit and end to end tests

I am writing tests for an algorithm that is built up of several consecutive stages. Each such stage has its own set of unit tests. I want to check that the whole algorithm performs as expected, but am ...
user avatar
  • 9
0 votes
3 answers
391 views

Should I test an all mocked method?

I have a similaire scenario: int retrievePrice(string productName) { string id = restApi.get("productid", productName); return restApi.get("productprice", id); } The restApi ...
user avatar
1 vote
2 answers
147 views

How to avoid unit test duplication in wrapper classes?

Assuming I’ve a class with three methods, startCollection, add, endCollection. One test case is: if start was not called, add should return an error. I’ve mocked the persistency storing the state with ...
user avatar
  • 141
0 votes
2 answers
125 views

Platform that converts Documentation into Unit/Integration Tests? [closed]

I've seen many SO questions around auto-generating unit tests, or documenting unit tests, but I'd like to go the reverse direction. What I'd REALLY like is to have folks (devs, tech writers, etc) ...
user avatar
  • 127
1 vote
2 answers
2k views

Is it a good practice to Mock entity manager in spring boot unit testing

I currently design an API using spring boot. In my service layer, I use Entity Manager for accessing the database. I have provided a method in my service layer below as an example. public Object ...
user avatar
3 votes
4 answers
325 views

Is design by contract useful without unit testing?

I read Bertrand Meyer's paper on design by contract yesterday and it is not very clear for me what is the relationship between DbC and testing, since it appears that without testing I cannot be ...
user avatar
  • 2,554
1 vote
1 answer
100 views

Python Unit Tests Mocking Imports - Removing Dependencies for CI/CD

I have a project written in python that I would like to create unit tests on. This project has a dependency on a database project which is a sort of abstraction layer to data connections. The issue ...
user avatar
3 votes
3 answers
266 views

TDD and Unit Testing for new code, but existing patterns

How should I go about using TDD and writing tests in this scenario? Suppose we have request handler classes. In short, a handler applies a "request" (e.g. Update details) to an entity then ...
user avatar
  • 147
1 vote
1 answer
199 views

How to unit test public method which internally calls many internal or private methods which are already individually unit tested [duplicate]

In our ASP.NET Core application, we have many APIs which are calling public method from the service class. Service class is implementing an Interface. Each service method (depending upon complexity ...
user avatar
  • 113
1 vote
1 answer
116 views

How to avoid messy variable dependencies for ease of testing in Node?

I couldn't decide if the question belongs on Stack Overflow, but I went with SE Stack Exchange for the higher quality, broader answers. Bare with me. Examples are in Node but I guess applies to a ...
user avatar

1
2 3 4 5
34