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
10 votes
1 answer
1k views

Have unit test generators helped you when working with legacy code?

I am looking at a small (~70kLOC including generated) C# (.NET 4.0, some Silverlight) code-base that has very low test coverage. The code itself works in that it has passed user acceptance testing, ...
Duncan Bayne's user avatar
10 votes
3 answers
2k views

What would help when refactoring a large method to ensure that I don't break anything?

I'm currently refactoring a part of a large codebase with no unit tests whatsoever. I tried to refactor code the brute way, i.e. by trying to guess what the code is doing and what changes wouldn't ...
Arseni Mourzenko's user avatar
9 votes
4 answers
2k views

In CI, should tests be run against src or dist?

I've had a dispute with a colleague about when to run JavaScript unit tests in a CI environment at one of two different times. Let's call the two parties PE (pro-early) and PL (pro-late). Early (...
Patrick McElhaney's user avatar
9 votes
2 answers
1k views

Respectable design pattern for making node modules flexible/testable?

I am looking to get some input from some more experienced testers than what I am. :) I am trying to make my node modules testable, allowing for dependency spying/stubbing/mocking without the need to ...
ctrlplusb's user avatar
  • 193
8 votes
4 answers
774 views

Dependency Inversion expands the API, results in unnecessary tests

This question has bothered me for a few days, and it feels like several practices contradict each other. Example Iteration 1 public class FooDao : IFooDao { private IFooConnection fooConnection;...
Chris Wohlert's user avatar
8 votes
3 answers
1k views

In test-driven development, are there good reasons not to write multiple failing tests and only then to make them pass?

Uncle Bob's three rules of test-driven development state the following: You are not allowed to write any production code unless it is to make a failing unit test pass. You are not allowed to ...
aef's user avatar
  • 321
8 votes
8 answers
3k views

Advantages and disadvantages to making common unit test scaffolding code

For the project my team and me are working on we often find that we need large pieces of scaffolding code. Creating domain objects with correct values, setting up mocks for repositories, dealing with ...
JDT's user avatar
  • 6,410
8 votes
2 answers
10k views

Should I bother to write unit test for UI/UX Components?

So I am building an application with Angular and have started to get into UI testing with DalekJS (http://dalekjs.com). As I have been writing these tests I have been thinking to myself, should I ...
ryanzec's user avatar
  • 2,777
8 votes
2 answers
919 views

Test Driven Development for Complex Games

I'm coding a game in my spare time, but I am mostly still a beginner when it comes to programming. I'm sorry if this question is off topic or if it ends up not being helpful to anyone else, but ...
bazola's user avatar
  • 255
7 votes
4 answers
1k views

Testing a Conversion class

I have a class that is responsible for performing conversions from/to twenty-something types. Let's call this class ConvertUtils. For C# programmers out there - this class expands .Net's Convert ...
Berlo's user avatar
  • 127
7 votes
4 answers
6k views

Are there any alternatives to dependency injection for stateless classes?

I am working on an application whereby I have designed classes to fit into several groups: Immutable: initialised through constructors, uses copy-and-swap idiom (inc. move), can deep copy (i.e. clone)...
Class Skeleton's user avatar
7 votes
2 answers
2k views

Static services and testability

Where is the figurative line drawn for using static services in a project? I am a coop student working and learning how to write .net MVC projects. I've been developing trying to stick to TDD. In my ...
Tenderdude's user avatar
7 votes
4 answers
17k 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, ...
herby's user avatar
  • 2,754
7 votes
3 answers
652 views

Unit Testing: Should we care about distinguishing between Mocks and Stubs?

There's been a lot of talk in various blogs, forums and on StackExchange about the distinction between Mock and Stub objects (most of them specifically referencing the Rhino Mocks framework). These ...
mjhilton's user avatar
  • 653
7 votes
3 answers
507 views

Is there a logical complexity beyond which TDD doesn't work?

All the books which talk about test-driven development (TDD) illustrate it with only very basic examples, less than 100 LOC. The algorithms I wrote using TDD were larger, but still relatively small in ...
Arseni Mourzenko's user avatar
7 votes
7 answers
3k views

Is mocking for unit testing appropriate in this scenario?

I have written around 20 methods in Java and all of them call some web services. None of these web services are available yet. To carry on with the server side coding, I hard-coded the results that ...
Vinoth Kumar C M's user avatar
7 votes
3 answers
4k views

Is it right to skip unit testing and go straight writing integration tests if there's no point of testing the unit in isolation?

According to Martin Fowler's article, https://martinfowler.com/bliki/TestPyramid.html It is advisable to write more unit tests than integration tests. Does this mean ideally that every unit of work ...
Xegara's user avatar
  • 181
7 votes
3 answers
1k views

Unit (regression) testing scientific algorithms given floating point behavior

I have been working on a project and running into a very difficult problem. The problem can be stated simply as how to unit-test numerical algorithms. However if you just took this simple statement ...
Jim Kramer's user avatar
6 votes
1 answer
603 views

Dependency injection: what belongs in the constructor?

I'm evaluating my current PHP practices in an effort to write more testable code. Generally speaking, I'm fishing for opinions on what types of actions belong in the constructor. Should I limit things ...
user avatar
6 votes
6 answers
1k views

Why are my unit tests so expensive? [duplicate]

I am a Java developer, working on a small project. We are three people in the team, we have a budget for 3 months of coding (+ some time for the Analyst, Project Manager and QA team). It is a small ...
Michal Krasny's user avatar
6 votes
7 answers
826 views

Is it appropriate to not follow the O/C principle if you have unit test coverage?

I find it more convenient to 'modify'existing classes rather than extending them. Thus, I violate the open closed principle of 'not modifying the compiled and tested source code, instead extending the ...
Anmol Gupta's user avatar
6 votes
2 answers
7k views

How much extra time is needed to add unit test in a development task?

My team is considering moving to test-driven development. Currently we have almost no unit testing, we have been only relying on the user acceptance test, and developer's own manual test. Not everyone ...
Louis Rhys's user avatar
  • 6,142
6 votes
1 answer
2k views

Wrapping utility classes and injecting them for unit testing purposes

I found that it is so hard to test classes that depend on other utility classes as java.nio.file.Files. It is also impossible to mock them using the classic unit testing stack (junit,mockito,..) ...
isqo's user avatar
  • 232
6 votes
2 answers
18k views

JUnit Testing in Multithread Application

This is a problem me and my team faces in almost all of the projects. Testing certain parts of the application with JUnit is not easy and you need to start early and to stick to it, but that's not the ...
Sebastian van Wickern's user avatar
6 votes
3 answers
3k views

Dependency Injection with default Construction

Most of my production code has fixed types of dependencies, illustrated below: the House for example, at run time, always depends on the same Kitchen. Therefore, I am wondering what I gain by having ...
user695652's user avatar
6 votes
4 answers
4k views

Does TDD formally use black box testing to supplement unit tests?

Unit tests are never perfect at capturing functionality, particularly in certain parts of an application (such as the GUI), so everyone needs some measure of black box testing. Does TDD have anything ...
user avatar
6 votes
3 answers
18k views

How should I test boolean function with many possible permutations

When I write an unit test I usually provide a context (plain object or mocked/stubbed object) that I setup in some ways and then I can run assert statement on the context: note: code is in pseudo-...
Guillaume's user avatar
  • 2,197
6 votes
2 answers
450 views

Unit testing - one test is partly a "superset" of another, is this wrong?

Wasn't sure how to phrase that title, sorry! I've just come across this in our code base, and was wondering what the consensus was about how to unit test it: C# (-ish, sorry, this is paraphrased ...
frumious's user avatar
  • 347
6 votes
5 answers
1k views

Are mocks in unit tests dangerous in dynamic languages?

I've started relying heavily on a mocking framework in php for my unit tests. My concern is that with a dynamic language, there is no way of enforcing a return type. When mocking, you have to ensure ...
GWed's user avatar
  • 3,135
5 votes
3 answers
6k views

Integration Testing: Testing Service to Service

I'm looking for some advice on testing strategies for service to service communication. I have one service (service A) that makes a call to another service (B) - which is a rest API. Both services ...
M.M's user avatar
  • 71
5 votes
5 answers
470 views

Unit testing coverage

How much of your code do you unit test? Do you try to get 100% coverage of everything or do you skip some classes? Background We've made a lightweight ORM layer which we have written plenty of tests ...
jgauffin's user avatar
  • 4,562
5 votes
2 answers
2k views

Should service test classes connect to the database [duplicate]

I want to know if the service test classes should use real DAO objects and connect to the database or it should use mock objects to test only the business logic it do?
Mohamed Ramadan's user avatar
5 votes
2 answers
318 views

How do you evaluate automated testing for a particular product/project?

Automated testing is pretty hyped-up in recent years, with particular emphasis on TDD at the "unit" level. The touted advantages include things like: Stabilizing existing code: breaking changes are ...
svidgen's user avatar
  • 15.1k
5 votes
3 answers
5k views

What is the best use case for selenium?

I've been trying to develop web applications while at the same time creating the testing. I understand unit testing, I can declare a test method and test specific methods in my application. But I don'...
Eddie Martinez's user avatar
5 votes
5 answers
2k views

"TDD is about design, not verification"; concretely, what does that mean?

I've been wondering about this. What do we exactly mean by design and verification. Should I just apply TDD to make sure my code is SOLID and not check if it's external behaviour is correct? Should ...
foobarcode's user avatar
  • 1,113
5 votes
2 answers
260 views

Is there a benefit to unit testing code whose sole purpose is to generate non-human-readable code in another language?

A significant portion of the application I work on every day consists of Javascript that outputs a lot of (what might as well be) Excel spreadsheet formulas. Yes, Excel is barely a Turing-complete ...
Ixrec's user avatar
  • 27.7k
4 votes
2 answers
3k views

Is this method of writing Unit Tests correct?

I have created a small C# project to help me learn how to write good unit tests. I know that one important rule of unit testing is to test the smallest 'unit' of code possible so that if it fails you ...
Theomax's user avatar
  • 701
4 votes
2 answers
864 views

Domain object model: query by id vs object

Let assume I have two simple model classes: Product and Brand It is obvious I have a query method in Product class like this Product product = Product.findById(123); What if, I want to query ...
Yoga's user avatar
  • 325
4 votes
3 answers
4k views

How can I write unit test case of file format converter utility?

I have created one utility module which converts one file format to another one. File i.e. test.abc will be converted to i.e. test.pqr and internal format of files are totally different. This module ...
Akash's user avatar
  • 405
4 votes
4 answers
687 views

Are injectable classes allowed to have constructor parameters in DI?

Given the following code: class ClientClass{ public function print(){ //some code to calculate $inputString $parser= new Parser($inputString); $result= $parser->parse();...
Songo's user avatar
  • 6,598
4 votes
4 answers
2k 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 ...
Adam Thompson's user avatar
4 votes
4 answers
1k views

What if I can't make my unit test fail in "Red, Green, Refactor" of TDD?

So let's say that I have a test: @Test public void MoveY_MoveZero_DoesNotMove() { Point p = new Point(50.0, 50.0); p.MoveY(0.0); Assert.assertAreEqual(50.0, p.Y); } This test then causes ...
Joshua Harris's user avatar
4 votes
1 answer
3k views

How to define implementation details?

In our project, an assembly combines logic for the IoC-Container, the project internals and the communication layer. The current version evolved to have only internal classes in addin assemblies. My ...
dwonisch's user avatar
  • 364
4 votes
8 answers
630 views

Testing loses its effectiveness if all programmers don't use them

Let's assume you are convinced that the extra time spent unit testing has merit and improves production. Does that still hold up when everyone working on the same code doesn't use them? This question ...
JeffO's user avatar
  • 36.8k
4 votes
2 answers
3k views

Does it matter how I setup test data when creating unit tests?

I have a unit test similar to the code snippet below, it should check that the AddUser method only allows unique emails. My question is around the Arrange part of this unit test, I use existing ...
user1786107's user avatar
3 votes
2 answers
350 views

.Net 8 XUnit: Should my tests use real data or how to mock MySql with CQRS?

I'm creating tests for our .Net 8 API. We use MySql for data storage and CQRS pattern for queries. In my personal opinion, I shouldn't use the real database for testing, because you will use an ...
Diego Perez's user avatar
3 votes
2 answers
784 views

Why is it not standard to implement abstraction layers for the file system?

I have been taught to access databases through abstraction layers. I was wondering why it is not also standard practice to access the file system through an abstraction layer? It seems to me unit ...
DudeOnRock's user avatar
  • 1,079
3 votes
5 answers
616 views

Testing From A Developer's Perspective [closed]

I have a book which mentions: "There are many types of testing, including unit testing, integration testing, functional testing, system testing, performance testing, and acceptance testing". It is ...
user2466019's user avatar
3 votes
1 answer
172 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 ...
user10869858's user avatar
3 votes
2 answers
545 views

Best practices for testing settings file with many parameters in simulation code

I'm conflicted as to what is the best way to approach this problem. I am writing a simulation in Python, which is parametrized by ~ 50 parameters. I have a JSON file where these parameters are set, ...
jfaccioni's user avatar
  • 506