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
6 votes
1 answer
14k 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 ...
Pampa Nello's user avatar
1 vote
2 answers
715 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 ...
user2037296's user avatar
0 votes
1 answer
151 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 ...
Jim_Mcdonalds's user avatar
1 vote
1 answer
157 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 ...
Qback's user avatar
  • 231
1 vote
2 answers
1k 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(...
kiss-o-matic's user avatar
1 vote
1 answer
697 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(): ...
Qback's user avatar
  • 231
2 votes
2 answers
924 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 ...
Johannes Hahn's user avatar
2 votes
2 answers
276 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 ...
user1415536's user avatar
-3 votes
1 answer
2k 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 ...
Mencia's user avatar
  • 113
4 votes
5 answers
6k 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 ...
fraiser's user avatar
  • 167
1 vote
2 answers
857 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 ...
Stephen Vernyi's user avatar
1 vote
1 answer
997 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 ...
Mario Ishac's user avatar
2 votes
3 answers
791 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 ...
jgv115's user avatar
  • 139
-1 votes
1 answer
639 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 ...
Eric's user avatar
  • 107
1 vote
3 answers
433 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 ...
JayZ's user avatar
  • 827
0 votes
2 answers
4k 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 ...
stubborn's user avatar
  • 193
-1 votes
3 answers
101 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() {...
moonwave99's user avatar
0 votes
2 answers
4k 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. ...
Alexander's user avatar
  • 4,954
1 vote
2 answers
439 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 ...
GreenyMcDuff's user avatar
3 votes
1 answer
1k 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 ...
Tal's user avatar
  • 141
27 votes
8 answers
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 ...
gogoolplex's user avatar
2 votes
1 answer
303 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 ...
totsubo's user avatar
  • 129
-2 votes
1 answer
143 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 ...
little raspy's user avatar
18 votes
1 answer
6k 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 ...
Jossy's user avatar
  • 311
-4 votes
1 answer
724 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 ...
KansaiRobot's user avatar
-3 votes
1 answer
180 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 ...
Antonis Karvelas's user avatar
0 votes
1 answer
2k 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 ...
Andrej's user avatar
  • 127
-2 votes
2 answers
310 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 ...
Ryn's user avatar
  • 125
0 votes
1 answer
54 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 ...
JobHunter69's user avatar
50 votes
8 answers
13k 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 ...
kibe's user avatar
  • 748
1 vote
5 answers
261 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 ...
Andrew Cheong's user avatar
4 votes
1 answer
4k 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. ...
user avatar
0 votes
1 answer
107 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 ...
Leonardo Mangano's user avatar
3 votes
4 answers
1k 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....
JobHunter69's user avatar
-5 votes
1 answer
95 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 ...
JobHunter69's user avatar
-2 votes
2 answers
4k 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 = ...
Bor's user avatar
  • 129
1 vote
3 answers
1k 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 ...
DodgyCodeException's user avatar
-1 votes
1 answer
559 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 ...
kibe's user avatar
  • 748
-3 votes
1 answer
216 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 ...
paul23's user avatar
  • 1,091
2 votes
2 answers
216 views

Ease testing with complex setup scenarios

Within our codebase we have integration tests like these: func TestActivateSuspendedFoo() { // setup all dependencies baz := jsonToStruct( "baz": { "a": ...
Filipe Gorges Reuwsaat's user avatar
-4 votes
1 answer
1k views

Is it possible to derive a test suite to achieve 100% path coverage? [closed]

Here is an example with the function: Examining this I believe that it is possible to achieve 100% path coverage. My understanding of path coverage is that you need to design test cases such that ...
Sri's user avatar
  • 13
3 votes
1 answer
324 views

What do you unit test in your angular applications?

I am currently working in a team, which, when I joined them did not do any sort of unit or integration testing. Over the last 2 years I have bit by bit pushed dotnet unit testing to a point where it ...
Web Dev's user avatar
  • 425
6 votes
2 answers
767 views

Is it a bad practice for a unit test to "trust" the other?

Assume I have the following two functions: function storeObject(object) { // Connect to database // Prepare query // Execute query } function retrieveObjectWith(id) { // Connect to ...
appa yip yip's user avatar
-2 votes
1 answer
66 views

Testing function that return objects

My web app is built around classes that I call widgets. Their goal is to be reusable and modular, to suit different scenarios. For example, I have a widget called BreadcrumbWidget which has two ...
user avatar
-1 votes
1 answer
267 views

How to do test-driven front end development?

I'm trying to rapidly develop my frontend, but every time I change my code I find myself refreshing my browser and running some macro to test whether the changes in my code solved the problem. I ...
libby's user avatar
  • 115
0 votes
1 answer
4k views

fake filesystem for unit tests

I am writing an application in c++ and Qt5. It would be very convenient for me to create a virtual file system so I can unit test code working on files. I have found that in Qt4 there was a ...
Michał Walenciak's user avatar
1 vote
3 answers
272 views

How does TDD behave when the tested unit needs to be expanded?

I want to write some invoicing logic, and I start coding it, using TDD. The following example is silly, but I'm confident it represents well the everyday dilemma I'm facing function ...
Vecchia Spugna's user avatar
1 vote
1 answer
3k views

C# | How to Unit test a Timer (in a daily job run scheduler)?

I wrote this class: using System; using System.Threading; using System.Threading.Tasks; public interface IJobScheduler { void RunDaily(Task task, int hour, int minutes ); } ...
Alex 75's user avatar
  • 111
-1 votes
2 answers
110 views

Code Review for Automated Unit Tests [duplicate]

Writing automated unit tests is followed as a part of our development process. We also do have an established code review process for the development code that is written. Should the test code be ...
Shylajhaa's user avatar
  • 107
0 votes
2 answers
2k views

What approach do I take to unit testing a class which has a method that internally calls other methods?

I have a class which has one method that is called from another class. This method internally calls several other methods to do its work. Those other methods are all public and can be called by the ...
Dhruv Prakash's user avatar

1
3 4
5
6 7
36