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.

28
votes
8answers
9k views

Methodology: Writing unit tests for another developer

I was thinking about software development and writing unit tests. I got following idea: Let's assume we have pairs of developers. Each pair is responsible for a part of the code. One from the pair ...
7
votes
3answers
400 views

How do I write unit tests for legacy code (that I don't understand)?

Forward I've read a lot of things before asking this question, including many relevant questions right here on SE: (Software Engineering SE) Writing tests for code whose purpose I don't understand (...
4
votes
3answers
1k views

Why the practice of writing unit tests in a different language isn't that popular?

When Microsoft released Visual Studio 2008, there was a thing they were talking a lot about at the conferences and in their online tutorials: the idea of writing the actual code in one language, and ...
54
votes
7answers
24k views

Is dependency injection essential for unit testing?

Is using dependency injection (DI) essential for unit testing? I can't think of another alternative for isolating code so it can be tested. Also, all the examples I have ever seen use this pattern. ...
1
vote
4answers
283 views

Is it bad practice to repeat logic being tested in unit tests?

Often I find myself writing a unit test for code and repeating some or a lot of the logic in the unit test to make the unit test DRY. For example, consider the following piece of code: function ...
5
votes
3answers
718 views

Change-detector tests considered harmful

I read this article from the google testing blog. Basically, the idea expressed in the article is that it's a good practice to avoid writing "change-detector tests". This code is given as an example ...
4
votes
1answer
226 views

Boundaries of unit testing between my code and a library or sub-component

Background: I am new to testing in general, and have been studying it in context of JavaScript, specifically React.js, front-end development (actually new to this as well). For the question, I have ...
2
votes
3answers
470 views

How to test required but accidental functionality?

For example, let's say I write a container class, which supports 2 kinds of operations: insert(x): inserts an element get_all(): retrieves all previously inserted elements Maybe it has some kind of ...
0
votes
3answers
134 views

Does this unit test violate DAMP?

In writing a split function that breaks up a sentence into each individual word based upon punctuation i.e. splits on something like the character class [\s,.?!]+, though for the purposes of this ...
3
votes
2answers
320 views

Should one make the database calls in the constructor or method of a class?

Take the following instance for example: CreditCardApplication class public class CreditCardApplication { public int Id { get; set; } public string FirstName { get; set; } ...
5
votes
5answers
2k views

How do you automatically test a time check?

Say you have a property startTime. Then you have a method doSomething: doSomething() { //...stuff startTime = System.currentTimeMillis(); //... more stuff } How do you test that startTime ...
4
votes
2answers
210 views

How to test database dependent functionalities?

I have written this function which checks if a user already exists in the application: async function ValidateUserExists(username, email){ if(!username || !email) throw new Error('Invalid number ...
0
votes
1answer
139 views

Do I need to unit test a generic method with all accepted types?

For a method whose signature looks like this: public T Add<T>(T first, T second) where T : struct, IEquatable<T>, IComparable<T> which can work with all of the integral types, do I ...
41
votes
14answers
7k views

Unit-testing of inherently random/non-deterministic algorithms

My current project, succinctly, involves the creation of "constrainably-random events". I'm basically generating a schedule of inspections. Some of them are based on strict schedule constraints; you ...
3
votes
4answers
502 views

Should test code be treated as normal code? [duplicate]

I remember reading on someone else's post that test code should not be treated as normal code but as small whole programs and that is an idea I really like and would like to refer to when discussing ...
1
vote
1answer
244 views

When writing tests for a WordPress plugin, should i run them inside WordPress or in a normal browser?

I have started using BDD for a WordPress plugin i'm working on and i'm rewriting the js codebase to do tests. I've encountered a few problems but i'm going steady now, i was wondering if i had the ...
-2
votes
4answers
314 views

Mocking dbContext vs Repository [closed]

I use entity framework with Repository Pattern. while searching for means of writing unit test cases that run isolated from the database I found some people mocking the database context and others ...
0
votes
1answer
95 views

Testing Stored procedure correctness

Suppose you have 2 stored procedure, let's say a GetProducts and a GetProductDetails. Now, the GetProducts returns a list of products with a property that is "calculated" basing on additional ...
3
votes
1answer
741 views

What is a good method of storing test data for development and unit testing with angularjs and jasmine?

What is a good method of storing JSON data/objects for development and unit testing with angular and jasmine? I have seen some ways of accomplishing this. However, since I am new to unit testing with ...
-1
votes
2answers
95 views

Is testing UI components considered to be unit testing? [closed]

My UI components are tightly coupled with my UI framework (i.e. Vue + Vuetify). The framework provides some testing utilities that allow you to fake the rendering of these components without the need ...
14
votes
4answers
4k views

Is it a good idea to measure the performance of a method by using unit test timeout?

In a project where there are non-functional requirements which specify the maximum execution time for a specific action, QA must check the performance of this action on a dedicated machine using ...
-2
votes
1answer
138 views

Unit tests branch best practices [closed]

I'd like advice about on the best practice for Pull Requests organizing on middle size project. Especially relating to Unit Testing. Currently we create branch for each feature, what is practically ...
0
votes
2answers
64 views

What to test when testing an API? [closed]

When testing an API (with, for example, Java), what parts should I actually be testing when calling methods of my Controller class (e.g. a Spring RestController)? For example, lets say I've got a ...
0
votes
2answers
95 views

Are refactorings expected to change unit tests that cover the functionality that is being refactored?

Imagine that you are using a tool that automatically applies a set of refactorings of your choice. If the refactorings breaks the existing API for whatever reason (e.g. change method name, ...
-2
votes
1answer
38 views

Documenting business logics of legacy application having no unit test

I have joined a team in company and this team owns few services, which are used widely by many other user facing internal/external UI apps and other internal apis. My manager gave me rough idea that ...
0
votes
1answer
66 views

Writing assertion for unit testing queue data structure

I have data structure queue with 2 operations: typedef struct queue queue void enqueue(queue *q, void *elem); void *dequeue(queue *q); queue *read_from_file(const char *path); I want to write ...
1
vote
2answers
62 views

Unit tests: tree of dependent tests

I frequently encounter situation where I have very basic tests, and if those fail, it doesn't make sense to run many other tests. Therefore I want to declare a tree of dependencies between my tests, ...
1
vote
2answers
56 views

Balancing function call overhead and testability in a code that is a part of the deep learning model training loop

I am currently implementing the transformer architecture for sequence to sequence problems. Key part of the model is the attention mechanism, which is basically a matrix multiplication, followed by a ...
0
votes
0answers
16 views

Should I do migration testing if the new library needs a different testing suite?

I'm using a pretty old library to make server requests which is not properly tested and it not really matching nowadays requirements, so the plan is to migrate to a new one over the next release. So ...
3
votes
4answers
172 views

Are there advantages to testing accessors separately?

If I were to have a class with a property on it should I be testing the get and set functionalities separately or together? class MyObject { public string Name { get; set; } } [TestClass] class ...
-2
votes
2answers
118 views

Which code to unit-test? [closed]

It is widely accepted, that UT's are necessary for any large project with automated testing. However, it is rarely the case, that 100% of the production code is covered by UT's. There are of course, ...
6
votes
4answers
7k views

How to unit test that the correct implementation of an interface is used?

I have this problem with unit testing for years. Unit testing should test isolated units, that is, if we use interface/implementation vocabulary, it should test concrete implementation classes. Fine, ...
-2
votes
1answer
188 views

Mock db out or not during testing?

I am writing an app that mainly consists of just creating, editing and reading records in database. I was persuaded that any app has to be covered with tests. I was told that it's a good practice to ...
0
votes
2answers
154 views

How to avoid ad hoc constructor for unit testing

According to the answer in this post Should my classes have separate constructors just for unit testing? ad hoc constructor is not good and should be avoided. Unfortunately, I have the same problem ...
33
votes
4answers
2k views

Add a unit test for each new bug

In my job all developers that resolve a bug have to add a new unit test that warns about this type of bugs (in the case it occours again). If a unit test is not possible (for example, a webpage design ...
1
vote
4answers
105 views

Unit Testing order with contained & inherited objects

I'm confused about unit testing order. Many posts in StackOverflow say that unit test should be independent and small. IMHO, in the case of aggregation or composition, the member objects should be ...
2
votes
3answers
153 views

Is it ok to write asserts on objects returned by a factory when the factory is the system under test?

I have code that follows this pattern. Class Factory build: returns: Add|Remove|Stub takes: property1, property2, property3 Class Add execute: does something takes: ...
0
votes
3answers
181 views

Unit test input values - validate or not?

I am manually creating a rather large data model as input to my unit tests. Data model is created using a number of builders. Developers use these builders in order to create the data model as they ...
1
vote
2answers
103 views

How to write unit tests a method with a result that is highly based on another method [duplicate]

How to write unit tests a method with a result that is highly based on another method ? async function getStatus( session, correlationId, data ) { const client = new SomeAPIWrapperClient({ ...
2
votes
1answer
70 views

Best practices on testing a function based on third party service

I'm trying to figure out how to create unit tests for a function, which behavior is based on a third party service. Suppose a function like this: def sync_check(): delta_secs = 90 now = ...
0
votes
1answer
89 views

Unit/Component testing using In Memory DB

I am writing Unit/Component test using In Memory DB. When I write Unit/Component test, I came across with the following question. I have the following two BL methods. ToCreate ToGet So when I ...
3
votes
1answer
123 views

How can the `log` function be used for formal verification

In JavaScript I want to create a log function: function log(string) { console.log(string) } Obviously this causes side effects; it prints to the screen. And I have no control over its ...
53
votes
16answers
6k views

Why not write all tests at once when doing TDD?

The Red - Green - Refactor cycle for TDD is well established and accepted. We write one failing unit test and make it pass as simply as possible. What are the benefits to this approach over writing ...
9
votes
4answers
5k views

What is black box unit testing?

I recently had my final exam for a software engineering course for my masters program and one of the questions on the exam was the following: Unit Testing is considered: a. White-box Testing b. Black-...
11
votes
3answers
8k views

Where should I draw the line between unit tests and integration tests? Should they be separate?

I have a small MVC framework I've been working on. It's code base definitely isn't big, but it's not longer just a couple of classes. I finally decided to take the plunge and start writing tests for ...
1
vote
1answer
57 views

Spring Boot Controller Unit Test Strategy

All of the controllers in our project are as dumb as we can make them. Rightfully so, they simply validate the incoming payload, hand the request over to a service layer, and return whatever response ...
4
votes
3answers
167 views

Is checking for unexpected types in dynamic languages in an untyped project necessary?

In statically typed languages or dynamically typed languages that use a type checking system you can guarantee that the input type is the type that you specified in the contract. However, in ...
-1
votes
1answer
78 views

Is there a best practice for storing certificates (e.g. x509), which include private keys, used in unit tests?

In a software library, I wrote, large parts of the code use x509 certificates for various puprposes like signing documents digitally. Is there a best practice for storing test certificates used for ...
15
votes
6answers
9k views

Parameterized tests - When and why do you use them?

Recently at work we've been having some differences of opinion with regard to Parameterized testing. Normally we use a TDD-style (or at least try to) so I understand the benefits of that approac. ...
-4
votes
1answer
291 views

What should I test with unit tests for data access layer? (MongoDB)

I'm working in TDD for my data access layer in MongoDB and I don't know what should I test? I think that I shouldn't test if the queries return what they have to return because It's a MongoDB concern....