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
16 votes
2 answers
1k views

Where to push a failing test?

I just changed branch settings on my GitHub repository, so that my [next] branch requires a passing CI build through a pull request. A discussion followed with a number of team members, about failing ...
Mathieu Guindon's user avatar
16 votes
6 answers
2k views

From a TDD perspective, am I a bad person if I test against a live endpoint instead of a mock?

I follow TDD religiously. My projects typically have 85% or better test coverage, with meaningful test cases. I do a lot of work with HBase, and the main client interface, HTable, is a real pain ...
sangfroid's user avatar
  • 3,219
15 votes
10 answers
1k views

At what point would you drop some of your principles of software development for the sake of more money?

I'd like to throw this question out there to interestingly see where the medium is. I'm going to admit that in my last 12 months, I picked up TDD and a lot of the Agile values in software development....
Martin Blore's user avatar
  • 4,645
14 votes
7 answers
10k views

Can the Strategy pattern be implemented without significant branching?

The Strategy pattern works well to avoid huge if...else constructs and make it easier to add or replace functionality. However, it still leaves one flaw in my opinion. It seems like in every ...
Michael K's user avatar
  • 15.5k
13 votes
5 answers
2k views

Is it useful to unit test methods where the only logic is guards?

Say I have a method like this: public void OrderNewWidget(Widget widget) { if ((widget.PartNumber > 0) && (widget.PartAvailable)) { WigdetOrderingService.OrderNewWidgetAsync(...
Vaccano's user avatar
  • 4,028
13 votes
2 answers
2k views

Adding unit tests to a legacy, plain C project

The title says it all. My company is reusing a legacy firmware project for a microcontroller device, written completely in plain C. There are parts which are obviously wrong and need changing, and ...
vgru's user avatar
  • 623
12 votes
3 answers
1k views

What should be tested in Javascript?

At work, we've just started on a heavily Javascript based application (actually using Coffeescript, but still), of which I've been implementing an automated test system using JsTestDriver and fabric. ...
Nathan Hoad's user avatar
12 votes
3 answers
6k views

Understanding Cyclomatic Complexity

I've recently come across Cyclomatic Complexity and I'd like to try to understand it better. What are some practical coding examples of the different factors that go into calculating the complexity? ...
VirtuosiMedia's user avatar
11 votes
3 answers
2k views

Is it a good idea to do TDD on low level components?

I'm considering writing a low level driver or OS components/kernels. The osdev.org folks seem to think that the important bits are not meaningfully testable this way, but I have read some discussions ...
Bill's user avatar
  • 8,310
11 votes
2 answers
381 views

Getting management approval to invest more in unit testing [duplicate]

I'm a big proponent of unit testing (I'd love to be involved in test driven development). The company I work for seems rather reluctant to spend much effort beyond largely manual end user testing ...
SDET's user avatar
  • 113
9 votes
7 answers
10k views

How do you check that code has been covered automatically?

I am in the process of setting up a Bamboo server for some new projects for a push to TDD in a CI/CD workflow. Sure, unit testing is great, but only as log as it is there. Now this might be better ...
Daniel Park's user avatar
8 votes
3 answers
1k views

Relative value of manual vs automated testing

The organisation I work for has recently employed a testing officer to run manual tests, but when I asked about being given time as a developer to write unit tests the response was that the manual ...
Simon Martin's user avatar
  • 1,006
8 votes
1 answer
2k views

Unit testing of "workflow" methods

We are currently developing a public API for our app. The methods on this public API are mostly putting together functionality in different corners of the system. An example would be: (names have been ...
maasg's user avatar
  • 550
8 votes
3 answers
3k views

Unit testing time-bound code

I'm currently working on an application that does a lot of time-bound operations. That is, based on long now = System.currentTimeMillis();, and combined with an scheduler, it will calculate periods of ...
maasg's user avatar
  • 550
8 votes
1 answer
3k views

Unit testing on visualization (3D graphics) frameworks

This is a follow up to this question. There I was asking how to do unit testing when you have a library of scientific algorithms. I have a similar problem now but with a different project. I'm ...
Alejandro Piad's user avatar
7 votes
4 answers
2k views

Do large test methods indicate a code smell?

I have a particular method called TranslateValues() (Cyclomatic-Complexity of 5) which I would like to test. The test requires a substantial number of mock objects which take up most of the method; ...
Ray's user avatar
  • 266
7 votes
5 answers
14k views

Is path coverage stronger than condition coverage?

I have encountered (even in literature) two contradicting opinions related to path vs condition coverage (not branch or edge!). Some say Path coverage is stronger than the condition coverage, some say ...
John V's user avatar
  • 4,888
7 votes
3 answers
5k views

What is the best practice for refactoring a static method in order to make it more testable?

Let's say you have a static method that looks something like this: public static bool Foo() { var bar = new Bar(); //do some stuff here } This method as it stands can be a real headache to ...
Joseph's user avatar
  • 533
6 votes
2 answers
774 views

Writing functional tests for a legacy project

I am trying to add a couple of tests to a legacy C project. The project basically consists of a command line tool that prints something to stdout every time an event happens. Now, since writing unit ...
user avatar
4 votes
7 answers
5k views

What is the objective of unit testing? [duplicate]

Possible Duplicate: Is unit testing or test-driven development worthwhile? I've been working with C# for the last 2 years, and I've never done any unit testing. I just need to know what the ...
Mou's user avatar
  • 209
3 votes
6 answers
796 views

Unit Testing with an Optimization Problem

Suppose I'm making an algorithm that identifies the subject of a picture. It could be anything that a computer doesn't do that well, but I'm not expecting to get the right answer every time - 80% is ...
user121330's user avatar
3 votes
2 answers
508 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 ...
KAT's user avatar
  • 140
3 votes
2 answers
5k views

Converting static utility class into singleton

In company where I work we have lots of "utility" classes, each has lots of code inside (thousands of lines), and they are all static. And one static methods call anothers. The problem here is that ...
maxpovver's user avatar
  • 131
3 votes
1 answer
325 views

Code Coverage or Test Brevity?

I've been writing a lot of unit tests lately and have become somewhat obsessed with code coverage. However, I'm struggling justifying going for 100% code coverage, when so many tests would be a ...
user1767270's user avatar
1 vote
2 answers
174 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({ ...
faressoft's user avatar
  • 119
1 vote
2 answers
2k views

Should I mock a Domain Service?

I am trying to decide whether to introduce mocks in my isolated Domain Model tests. I have a class method similar to this: public Offer AssignOffer(OfferType offerType, IOfferValueCalculator ...
w0051977's user avatar
  • 7,003
1 vote
2 answers
2k views

Why is white box testing discouraged in OOP?

It seems the general consensus for unit testing classes is to test your object through its public interface only. So if you wanted to test the removeElement method on a LinkedList class you'd need to ...
user avatar
1 vote
1 answer
5k views

Is it worth writing a unit test for a DTO with the most basic getter/setters? [duplicate]

The advantage is it protects your DTO against future "enhancements" ?
NimChimpsky's user avatar
  • 4,617
0 votes
3 answers
622 views

What concept am I missing with private methods and testing? [duplicate]

I've read a lot o blogs arguing about private methods and testing. Some people say you should not test private methods, they say you should make them public or put those methods in a new class. But ...
marcos.borunda's user avatar
0 votes
1 answer
1k views

How to not test implementation when method returns void?

Many places in stackexchange state that you should not unit test implementations, only the public interface of a class. But what if the public interface is a method that doesn't return any value, for ...
BornToCode's user avatar
  • 1,273
-1 votes
6 answers
316 views

Cheap implementations in fundamental TDD

This is a question concerning the fundamental approach of TDD, so the example below is as simple as possible which might make it seem a little useless; but of course the question applies to more ...
Alfe's user avatar
  • 231
-2 votes
2 answers
120 views

How to deal with (apparently ?) contradictory testing "good practices"? [closed]

The contradictory "practices" I'm talking about are : Have a pyramid of tests (with many more unit tests than high-level tests), because high level tests are harder to maintain and slower. ...
Tristan's user avatar
  • 173
160 votes
13 answers
34k views

Is there such a thing as having too many unit tests?

I have been tasked with writing unit tests for an existing application. After finishing my first file, I have 717 lines of test code for 419 lines of original code. Is this ratio going to become ...
user2954463's user avatar
  • 1,430
130 votes
15 answers
23k views

How do you write unit tests for code with difficult to predict results?

I frequently work with very numeric / mathematical programs, where the exact result of a function is difficult to predict in advance. In trying to apply TDD with this kind of code, I often find ...
JustinLovinger's user avatar
118 votes
8 answers
42k views

How exactly should unit tests be written without mocking extensively?

As I understand, the point of unit tests is to test units of code in isolation. This means, that: They should not break by any unrelated code change elsewhere in the codebase. Only one unit test ...
Alex Lomia's user avatar
  • 1,281
109 votes
12 answers
18k views

Is testable code better code?

I'm attempting to get into the habit of writing unit tests regularly with my code, but I've read that first it's important to write testable code. This question touches on SOLID principles of writing ...
WannabeCoder's user avatar
  • 2,784
96 votes
19 answers
33k views

Why are unit tests failing seen as bad?

In some organisations, apparently, part of the software release process is to use unit testing, but at any point in time all unit tests must pass. Eg there might be some screen which shows all unit ...
user619818's user avatar
  • 1,757
84 votes
11 answers
11k views

Shouldn't unit tests use my own methods?

Today I was watching a "JUnit basics" video and the author said that when testing a given method in your program, you shouldn't use other of your own methods in the process. To be more specific, he ...
carlossierra's user avatar
  • 1,395
67 votes
7 answers
46k 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. ...
Tom Squires's user avatar
  • 17.7k
62 votes
4 answers
20k views

TDD - Outside In vs Inside Out

What is the difference between building an application Outside In vs building it Inside Out using TDD? These are the books I read about TDD and unit testing: Test Driven Development: By Example Test-...
Songo's user avatar
  • 6,548
55 votes
6 answers
13k views

Should unit tests be stored in the repository?

I'm a growing programmer who's finally putting unit testing into practice for a library that I'm storing on GitHub. It occurred to me that I might include the test suites in the repo, but as I look ...
parisminton's user avatar
52 votes
12 answers
5k views

If your unit test code "smells" does it really matter?

Usually I just throw my unit tests together using copy and paste and all kind of other bad practices. The unit tests usually end up looking quite ugly, they're full of "code smell," but does this ...
Buttons840's user avatar
  • 1,856
52 votes
2 answers
11k views

How should C++ Unit Test code be organized for maximum Unit Test efficiency?

This question is not about Unit Testing Frameworks. This question is not about writing Unit Tests. This question is about where to put the UT code written and how/when/where to compile and run it. ...
Martin Ba's user avatar
  • 7,363
39 votes
11 answers
9k views

Unit testing methods with indeterminate output

I have a class that is meant to generate a random password of a length that's also random, but limited to be between a defined min and max length. I'm constructing unit tests, and ran into an ...
GordonM's user avatar
  • 6,285
37 votes
10 answers
9k views

In TDD, should I add unit tests to refactored code?

While refactoring my code using Test Driven Development (TDD), should I keep making new test cases for the new refactored code I am writing? This question is bases on the following TDD steps: Write ...
ianmandarini's user avatar
  • 2,718
35 votes
6 answers
4k views

How should you TDD a Yahtzee game?

Let's say you're writing a Yahtzee game TDD style. You want to test the part of the code that determines whether or not a set of five die rolls is a full house. As far as I know, when doing TDD, you ...
Kristof Claes's user avatar
33 votes
4 answers
41k views

Is it bad practice to make methods public solely for the sake of unit testing? [duplicate]

I have a class with a public method. It has other methods that 'assist' with the purpose of the public method. The public method must be tested. However, I also want to unit test the private methods. ...
JᴀʏMᴇᴇ's user avatar
31 votes
4 answers
13k views

Is Functional Programming a viable alternative to dependency injection patterns?

I have recently been reading a book entitled Functional Programming in C# and it occurs to me that the immutable and stateless nature of functional programming accomplishes similar outcomes to ...
Matt Cashatt's user avatar
  • 3,305
31 votes
4 answers
12k views

Should I include tests in Docker image?

When it comes to tests, I can think of two options: Put both test and application in one image. Include only application code in the image. Create a test-specific container that builds after the ...
lfk's user avatar
  • 419
30 votes
8 answers
5k views

Value of using private methods vs all public in classes for internal software for ease of unit testing [duplicate]

Here's a skeleton of a class I built that loops through and deduplicates data - it's in C# but the principles of the question aren't language specific. public static void DedupeFile(FileContents fc) {...
Bob Tway's user avatar
  • 3,576