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.
271
questions
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 ...
8
votes
2
answers
905
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 ...
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 ...
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 ...
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 ...
7
votes
3
answers
3k
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 ...
7
votes
4
answers
5k
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)...
7
votes
3
answers
644
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 ...
7
votes
3
answers
453
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 ...
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 ...
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 ...
6
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 ...
6
votes
1
answer
594
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 ...
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 ...
6
votes
4
answers
16k
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, ...
6
votes
2
answers
439
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 ...
6
votes
3
answers
2k
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 ...
6
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 ...
6
votes
3
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 black box testing. Does TDD have anything to ...
5
votes
6
answers
3k
views
Is testing behavior of many classes in one test still unit testing?
Our project's policy is to write unit tests for single classes only. All dependencies are mocked. Recently we've noticed that this approach makes us vulnerable in such cases:
Originally class A looks ...
5
votes
7
answers
808
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 ...
5
votes
5
answers
1k
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 ...
5
votes
2
answers
255
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 ...
5
votes
2
answers
314
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 ...
5
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,..) ...
5
votes
3
answers
17k
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-...
5
votes
5
answers
460
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 ...
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?
5
votes
2
answers
17k
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 ...
5
votes
2
answers
9k
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 ...
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'...
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 ...
4
votes
2
answers
2k
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 ...
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 ...
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 ...
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 ...
4
votes
2
answers
781
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 ...
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 ...
4
votes
4
answers
551
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();...
4
votes
8
answers
610
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 ...
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.
...
3
votes
2
answers
357
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, ...
3
votes
1
answer
152
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 ...
3
votes
2
answers
736
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 ...
3
votes
2
answers
914
views
Is it acceptable to test based on test output data rather than input data in unit tests?
I'm used to write unit tests with assertions which are based on the input, e.g. (hopefully self-explanatory and let's assume that using random test data is fine)
int a = random();
int b = random();
...
3
votes
5
answers
612
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 ...
3
votes
2
answers
718
views
Term for unit testing that separates test logic from test result data [closed]
So I'm not doing any unit testing. But I've had an idea to make it more appropriate for my field of use. Yet it's not clear if something like this exists, and if, how it would possibly be called.
...
3
votes
2
answers
894
views
Unit test and Code Coverage of Ant build scripts
In our development environment We have more and more build scripts for ant to perform the build tasks for several different build jobs.
These build scripts sometimes become large and do a lot of ...
3
votes
6
answers
4k
views
Can unit tests verify software requirements?
I have often heard unit tests help programmers build confidence in their software. But is it enough for verifying that software requirements are met? I am losing confidence that software is working ...
2
votes
5
answers
7k
views
Should automated unit tests be part of the build?
Should automated unit tests be a part of the build process, or should they be manually run when someone makes changes to the code instead?
To me it seems like making it part of the build rather than ...