Skip to main content

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
2 votes
1 answer
644 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 ...
Ash's user avatar
  • 131
-2 votes
1 answer
166 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 ...
Stack's user avatar
  • 151
5 votes
2 answers
692 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 ...
Erik Mm's user avatar
  • 59
9 votes
5 answers
1k 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 ...
George Z.'s user avatar
  • 705
2 votes
4 answers
842 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 ...
DaCool1's user avatar
  • 37
0 votes
3 answers
118 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: '...
Jose Daniel Vivar Personat's user avatar
-1 votes
1 answer
499 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 ...
cluelessatthis's user avatar
0 votes
1 answer
930 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. ...
MrBit's user avatar
  • 233
2 votes
3 answers
994 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 ...
de1337ed's user avatar
  • 165
1 vote
1 answer
1k 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 ...
Riz's user avatar
  • 206
2 votes
2 answers
244 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 ...
dendog's user avatar
  • 201
0 votes
1 answer
154 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. ...
Eric Hernandez's user avatar
0 votes
2 answers
806 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 ...
Byebye's user avatar
  • 337
2 votes
1 answer
1k 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 ...
Herr Derb's user avatar
  • 439
-1 votes
2 answers
122 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 ...
Josu Goñi's user avatar
-2 votes
2 answers
2k 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 ...
RTT's user avatar
  • 1
36 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 ...
Nikolas Charalambidis's user avatar
2 votes
2 answers
1k 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 ...
Wballer3's user avatar
0 votes
1 answer
916 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: ...
pob's user avatar
  • 109
0 votes
1 answer
287 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 ...
3m3sd1's user avatar
  • 59
1 vote
3 answers
909 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(): ...
CommanderPrice's user avatar
7 votes
4 answers
3k 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 ...
Magnus's user avatar
  • 195
1 vote
1 answer
160 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)) ... ...
Wballer3's user avatar
2 votes
1 answer
2k 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? ...
de1337ed's user avatar
  • 165
1 vote
0 answers
146 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 ...
Anders Bernard's user avatar
0 votes
1 answer
64 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 ...
krezno's user avatar
  • 167
0 votes
3 answers
470 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 ...
Olivier D's user avatar
1 vote
2 answers
446 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
0 votes
2 answers
141 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) ...
aronchick's user avatar
  • 127
1 vote
2 answers
10k 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 ...
Prasad Darshana's user avatar
4 votes
4 answers
656 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 ...
edalorzo's user avatar
  • 2,656
1 vote
1 answer
351 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 ...
Simon Nicholls's user avatar
3 votes
3 answers
350 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 ...
ilitirit's user avatar
  • 147
2 votes
1 answer
3k 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 ...
WAQ's user avatar
  • 123
1 vote
1 answer
177 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 ...
JuliusL33t's user avatar
0 votes
0 answers
141 views

Mock/Reality consistency : unit test or integration test?

In a golang project, I have mocked external services. But in order to check that mocked services has the same behaviour as real services, I have done some common testing on the interface with both ...
Lerenn's user avatar
  • 101
1 vote
1 answer
243 views

How do I test a user receiving a text?

I am writing functional tests for my application that scrapes some data online then sends the results in an SMS text (on a weekly basis). My understanding is that functional tests are meant to test ...
Cherise's user avatar
  • 69
5 votes
4 answers
1k views

With Kent Beck's "test && commit || revert" (TCR), do you skip the "red" of "red, green, refactor"?

If you haven't heard of Kent Beck's TCR, it can be summarized with this: any time your tests go green, you commit; anytime your tests go red, you git reset --hard. This post is about how to practice ...
Daniel Kaplan's user avatar
1 vote
2 answers
1k views

Should "functional core, imperative shell" approach canonically break "don't test private methods" rule?

In short, "functional core, imperative shell" can be summarized as: functional core implements logic; you unit test it. Your tests call real functions just like in production real functions ...
Coderino Javarino's user avatar
4 votes
3 answers
2k views

Should I make my class mockable by marking its methods as virtual or by creating an interface?

In the process of refactoring non-testable code we are re-designing some of ours classes so that it can be easily replaced by a stub or mock during unit tests. Here is an example of such class (...
Delgan's user avatar
  • 366
1 vote
2 answers
9k views

How to Unit test a class that saves a file?

I am writing a class that has one function that I want to test. The function, textToPdf, takes a list of text files and creates and saves a PDF at the file path argument. In order to test that this ...
Startec's user avatar
  • 157
-1 votes
1 answer
52 views

In the following example, would the public properties be considered Dependencies/Collaborators

In the code sample below. Would UserId, Email, and UserType be considered "dependencies" with respect to the ChangeEmail method? would they be considered "collaborators"? Is it a ...
3m3sd1's user avatar
  • 59
2 votes
3 answers
241 views

Given a function that has already been unit tested, but is present in another unit, should I bother testing it again?

Say, for instance, I have this simple function in my domain layer: function canCreateNewUsers (principal: User): boolean { return principal.isSuperAdmin || principal.isAdmin // || ... a bunch of ...
kibe's user avatar
  • 748
1 vote
3 answers
3k views

Should I unit test "internal" classes?

In a server application, I have a class called Controller which receive the incoming request. I also have a class called Service which do business logic and a class called Database. All classes are &...
Olivier D's user avatar
1 vote
2 answers
630 views

Is there a simple algorithm for generating unit tests given a function's code? [closed]

Given the abstract syntax tree (AST) of each line of a function's code, I am asked to generate code for that function's corresponding unit tests, similar to what Microsoft's IntelliTest tool does here:...
David Johnson's user avatar
1 vote
4 answers
120 views

Should I public method and test it instead of testing a caller

I have a method called GetValueA() and GetValueB() which return string that in formatted public string GetValueA(){ string a = service1.GetA(); return Format(a); } public string ...
weanich sanchol's user avatar
1 vote
2 answers
104 views

Where shall we start mocking?

Scenario: Our CLI-script downloads data Therefore, amongst other things such as pre/postprocessing, it calls a function from another (internal) python package (which is maintained by another group) ...
gebbissimo's user avatar
53 votes
10 answers
7k views

How should I test "Glue Functions" without testing that "the code I wrote is the code I wrote"?

I usually write my code in a test driven style. I write tests as specifications and then my code. It's great and useful. I always try to ignore implementation when testing and only test behaviour. I ...
Derek C.'s user avatar
  • 617
1 vote
4 answers
3k views

How to effectively unit test code with lots of database dependencies?

I find myself writing a lot of boilerplate mocking code for my unit tests. I think there must be a better way. Background I am working on a project that relies on complex configuration that is stored ...
srk's user avatar
  • 127
66 votes
5 answers
21k views

Is the usage of random values in unit testing a good practice? [duplicate]

Having worked in complex solutions that had Unit Tests and Integration Test in the CI/CD pipeline, I recall having a tough time with tests that failed randomly (either due to random values being ...
Vinicius Scheidegger's user avatar

1 2
3
4 5
36