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
3answers
150 views

How to write unit tests for REST API that are atomic?

I am writing a REST API for use by the front-end web and mobile frameworks. Naturally, I write unit tests for each endpoint, which makes developing, evolving, and maintaining the API easier. However, ...
-1
votes
1answer
97 views

Java: Splitting a large unit test class

The project (Java/Spring) I currently work on has a rather large unit-test class for one of its services : more than 1000 lines, several Nested class (one per big functionality), some tests without a ...
0
votes
2answers
82 views

Unit testing multiple features in one test

I'm writing a small utility class to create an abstraction for addEventListener (JavaScript/DOM) Let's say I have to test for these features: Single event target with a single event Multiple events ...
-4
votes
0answers
24 views

using json for unit-tests input and outputs instead of builders and assertions?

Background: I'm writing unit-tests for a java data-intensive application. The application's input are user defined database tables that can contain all data types (numeric-types, string-types, date-...
4
votes
2answers
210 views

Doesn't “Always test through the public interface” contradict testing of individual composed functions?

I'm currently reading "Composing Software" by Eric Elliott, which is about functional programming in JavaScript. He states that if you compose multiple functions together, and that these ...
-1
votes
3answers
87 views

Should methods with business logic be made private? [duplicate]

So I am writing a project using Spring Boot. All of my logic resides in @Service classes. I have separated each service class based on entity. For example - If I have two independent entities A and B, ...
-3
votes
1answer
33 views

Testing a React component which receives a function as a prop

Suppose there's a root <App /> which houses multiple components, one of them being, say <SearchBox />. App component houses the app state (such as searchResults array) and iterates the ...
50
votes
11answers
10k views

If two individual branches pass unit tests, once they're merged, is the result also guaranteed to pass unit tests?

Suppose we have two branches A and B which have been forked from master. Both branches A and B make some changes and implement some unit tests. They pass all current and new tests, then are merged ...
1
vote
2answers
94 views

Unit testing a function that composes filters

Imagine there is a number of filter functions that all perform filtering of items in a list based on different criteria. Say there are 5 such filter functions. All unit tested Now imagine there is ...
1
vote
4answers
196 views

Should Unit Tests share the Same Data set?

We are writing application service tests across our Data Service Class. Currently writing tests liking add Product, remove Product, check for inventory after sale, etc. They are all using the same ...
1
vote
0answers
79 views

Should I mock ObjectMapper in my unit tests?

I have different services in a spring application that have a dependency on Jackson ObjectMapper, the unit tests rely on @InjectMocks to inject all the various dependencies to the class that is under ...
1
vote
2answers
192 views

Unit Testing Private Function Through Public Interface

Am new to unit testing, and I have been struggling with the right approach to test private functions. I have done my research, and am going with testing through the public interface. My problem is the ...
0
votes
1answer
45 views

Draw Data Flow Graph with Constructor and Functions

I am new to software testing and would like to know whether it's appropriate to draw a data flow graph that flows from initiation of variables to constructor and method. Is there something wrong with ...
1
vote
1answer
96 views

Should I stub hidden dependencies in Python unit tests

Python is very specific language which gives developer huge flexibility. So if I have class like this: class Car: def __init__(self, engine: Engine): self._engine = engine # dependency ...
1
vote
2answers
104 views

C++ Organizing classes for easy unit testing (but with some restrictions)

Mild pickle. I have a project which has components that are difficult to test/mock. It might look something like this: class Contenxt; class Server : public SomeOtherClass { public: ServerPlugin(...
1
vote
1answer
104 views

How deep should I mock dependencies in unit tests

Consider the following function: def get_api_status(api_client): response = api_client.get('/status/') return response.content and the test for it: def test_get_api_status(): ...
2
votes
2answers
158 views

Migrating legacy code with singletons to Dependency injection

I'm working on a larger, older project. Our code is littered with classical singletons, i.e. classes like public class ABCService { private static final instance = new ABCService(); public ...
2
votes
2answers
190 views

Unit-testing and mocking

There seems to be a lot of question regarding "when to mock". But I did not get an answer on my question so far. It can be, I do not know a specific search request that would point me to the ...
-3
votes
1answer
55 views

Test to check that executing a function does not raise an exception

I have the following function: def create_dic(): key_list = ['a','b'] val_list = [1, 2] dic = {} for i in range(2): dic[key_list[i]] = val_list[i] return dic I ...
1
vote
5answers
407 views

Should unit tests contain print statements?

I am running unit tests written by someone else, and the output is littered with print statements. I know libraries are not supposed to contain print statements, but is it bad practice for tests to ...
0
votes
2answers
155 views

What is the correct way to unit test methods that mix dependencies and business logic?

I'm struggling to come to a consensus on the right approach that can be used somewhat consistently to balance integration and unit testing. Take the following method, that is extremely common to find ...
1
vote
1answer
108 views

Benefits of Dynamic Mocks over Static Mocks?

I'm not sure if static vs. dynamic mock is the terminology used to describe this comparison, but I got this terminology from types of mocking static vs dynamic and Hand-rolled mocks made easy. To ...
2
votes
3answers
343 views

How to not violate the single responsibility principle?

Let's say I'm writing a class that tracks a single stock ticker. In this simplified example, the StockTracker class holds a string that tells me the trend direction and a variable window that holds x ...
0
votes
0answers
34 views

How to create input data for unit tests for an API client?

I am building the initial set of unit tests for my team's legacy API client system. We have been writing integration tests, but have no unit tests. It's a Sinatra server that accepts requests from our ...
1
vote
3answers
190 views

Refactor unit tests?

When we work with legacy code and need to do changes, we first write tests on the current behavior. That way we can implement new changes with confidence. We can even refactor the code. Legacy code is ...
-2
votes
2answers
148 views

In unit testing should I check if a method is being called or just check the result?

Suppose I have similar function: Helper2 helper2; public void myFunc(Helper helper) { if (....) helper.mySecondFunc(); else helper2.myThirdFunc(); } In my unit test should I ...
-1
votes
3answers
92 views

Should branching of functions be tested if not part of the contract?

Let's assume I have a method that performs an internal operation just the first time it gets called, e.g.: class Foo { constructor() { this._unlocked = false; this._data = null; } bar() {...
-1
votes
2answers
183 views

Unit testing private methods

preface: I know this topic has been asked about a lot on here in the past. Hopefully I will make it clear that I've read a fair amount of the questions/answers on the topic, and other literature, too. ...
1
vote
2answers
102 views

Unit Testing a class that requests data from multiple sources

Context I'm working on a project that pulls data from AWS using the various AWS SDKs for .NET. This specific example deals with the AWSSDK.IdentityManagement SDK The goal is to query information from ...
3
votes
1answer
49 views

How to test channel pipelines in Go

I use the "channel pipeline" pattern quite a lot in Go, which looks something like this: // getSomeNums spits out ints onto a channel. Temperatures, pressures, doesn't matter func ...
26
votes
8answers
5k views

Does software which implements scientific models require unit tests?

I work in a field where lots of code is written, but hardly ever tested. This is because we are foremost scientists who try to solve problems with code. The few coding courses we had, focused on the ...
2
votes
1answer
113 views

Changing a class with static dependency injection to allow unit testing

I'm new to JUnit/Mockito and to unit testing in general. I'm asking this question in order to get feedback and learn best practices/patterns/strategies. I wrote a class but when came time to unit test ...
-2
votes
1answer
50 views

How to properly ensure equal functionality of different implementations?

I've got a project where I have several implementations which are stateless and should provide the same results for the same input (but use different internal APIs) and need therefore be tested to ...
15
votes
1answer
4k views

Is it possible/advisable to combine unit testing and integration testing?

I've built a Python script that consists of about 20 functions that extract data from MySQL via .sql files (I'm currently trying to learn SQLAlchemy ORM), do something to it and then put it back in ...
-4
votes
1answer
65 views

Are google tests used only for unit tests?

The current system I am working on uses google tests for unit tests and pytest for system tests. Are google tests appropriate only for unit tests? Or can you write system tests with it? (or are pytest ...
-3
votes
1answer
45 views

REST API Testing best strategy

I'm currently developing a personal project using Django REST + React and as it grows more complex, I want to add unit testing to ensure that changes don't affect previous functionality. However, I'm ...
1
vote
1answer
150 views

Test driven development on front end?

When reading through internet, I've seen people are really into testing the front end applications. Some of them also say that they will never hire someone on front end who doesn't have testing ...
-2
votes
2answers
104 views

Should you write integration tests at every level?

Suppose I have some code as such: function1() { function2() } function2() { function3() } function3() { function4() } function4() { ... } Do I have to write integration tests for ...
-1
votes
1answer
37 views

Should all third party methods that access outside resources (like other databases) be wrapped up?

From the perspective of unit testing, the code under test should obviously not be accessing outside resources so the third party methods need to be mocked. However, it seems like this is poor practice ...
41
votes
7answers
8k views

How do I really write tests without mocking/stubbing?

I have been using TDD when developing some of my side projects and have been loving it. The issue, however, is that stubbing classes for unit tests is a pain and makes you afraid of refactoring. I ...
1
vote
5answers
197 views

Hypothetically if every scenario were covered by an end-to-end tests, would unit tests still have any value?

Note: I'm asking about the strategy behind unit / integration / end-to-end tests, not about classifying tests as one or the other. More so in the past than present, it was expensive to write and run ...
3
votes
1answer
667 views

Clean Architecture use case testing

Use case interactors in the Clean Architecture consists of the application specific business rules. Interactors uses the Data Access Interface to fetch the required data from the data access layer. ...
0
votes
1answer
66 views

Asserting against Fakes

If I have a IRepository that Create,Update,Delete and Get an entity from the database and I do a fake for it with a public List inside it. If I am testing a command that is using it: is a bad ...
2
votes
4answers
274 views

How do I deal with the fact that I am forced to make helper functions public for testing purposes?

I've encountered several scenarios that require me to mock certain helper methods because they call outside resources. As a result, I'm forced to convert all my helper methods from private into public....
-5
votes
1answer
41 views

Should code be refactored to be more unit testable or should a framework like PowerMock be used?

Assume there's some code that's already in production that needs some unit testing. Generally speaking, would you want to refactor this code that's already in production by adding things like adding ...
-2
votes
2answers
168 views

Unit testing for creating a file with a timestamp in the name, for Java/Scala

What is a good way to test whether particular file, having a timestamp in the name is created? def writeFileOnDisk(fileName: Sting, outpuPath: String ): Unit = { val saveFileNamePth = ...
0
votes
1answer
51 views

What to do with a legacy code base with no unit tests and complex architectural structure [duplicate]

I have been working for my company for almost a year now and been primarily focused on adding features and maintaining two 15+-year-old WPF Projects and one 20+-year-old WinForms Project. The ...
1
vote
3answers
112 views

Mock a private dependency

I've started developing a Java API which will consist of just a couple of public classes, a public interface (to be implemented by the user and used as a callback, like in the Observer pattern). All ...
-1
votes
1answer
82 views

Is the self-shunt pattern the same as stubbing in TDD?

I am reading the book TDD by Example and one of the patterns demonstrated was the self-shunt pattern. Basically, if I am not mistaken, if I have a test like this: test("list of users should receive ...
-3
votes
1answer
82 views

Is splitting unit tests from integration test with mocks worth the effort (in nodejs)?

Well consider a relative simple server for a SPA application. Written in nodejs with express and knex as backends. Now if we do it properly each function will have unit tests (as they are always all ...

1
2 3 4 5
32