Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

1
vote
1answer
162 views

How to test controllers in a REST applications

I'm writing a REST api backend in NODE js, which is based on MVC approach. My api routes directly hit the controllers, which in turn import models which are a wrapper around the methods from a library ...
2
votes
3answers
226 views

Should I test the debug branch of my code in a unit test?

Lets say I have a function that gets a list of what files to send that are later send over a socket to some other pc. Now usually I keep track of what files have been sent and don't send these files ...
0
votes
1answer
109 views

How do I create an integration test that tests multiple microservices without mocking?

I understand the concept of mocking API responses and their uses in unit testing, but in functional/integration testing, I actually don't want to mock because that defeats the purpose. For example, ...
1
vote
6answers
532 views

How can I unit-test the presence of specific file contents, through an interface?

I am currently trying to test a file manager class. This class is responsible for keeping track of how long the file is good for. The file will have a date written into it that denotes when it was ...
0
votes
1answer
200 views

If I am mocking API responses in my unit/integration tests, how do I guarantee that the contract between Service A and B are valid?

Let's say I have microservice A and B. Microservice A calls microservice B for some piece of information. Microservice B requires parameters 1, 2, and 4 for the request to be valid. If I'm simply ...
1
vote
2answers
394 views

How do you unit test a microservice that makes a call to another microservice?

I have a suite of microservices: ConnectorRegistrar ConnectorTypeA ConnectorTypeB ... ConnectorTypeZ "Someone" can make a call to the ConnectorTypeA microservice to create a connector of type A, and ...
1
vote
4answers
233 views

Little value in unit-testing the database component

Having a component that represents the database is wonderful! You can use it from the business logic to read and write data. But should that component be unit-tested? I would argue that it should ...
1
vote
1answer
165 views

How to unit test high-level abstractions that transform complex Data

I'm practicing unit testing (specifically Test-driven design or Red-Green-Refactor) while writing a recursive descent parser for a simple domain-specific language. In trying to unit test a method that ...
1
vote
1answer
79 views

Keeping Stubs in Sync?

If I want to test frontend code (e.g. react SPA) that queries the backend and I stub out the responses from the backend using sinon fake server/fake XHR, then what happens if the backend code changes? ...
0
votes
3answers
168 views

Unit Testing method with multiple steps

I have a validation method like this def validate(a, b, c, d, e, f): Boolean = { val rs1 = check1(a, b) val rs2 = check2(c, d) val rs3 = check3(e, f) rs1 && rs2 && rs3 } I ...
0
votes
1answer
81 views

Client-Side TDD Tests?

More specifically, with regards to ATDD (sort of BDD flavored, or some could argue what was actually intended in the first place by TDD) should be there much UI testing? As I have been testing my UI ...
4
votes
4answers
801 views

What is the most concise way to test a boolean condition with many AND clauses?

I have a function which returns the availability of a module, where the module is only available if multiple conditions are all met. The code looks like this: bool isShipAvailable() { bool ...
8
votes
2answers
368 views

Philosophy on unit testing daisy-chained methods?

Problem Statement: I have a class that does some validation and it looks something like this: func a() {b()} func b() {c()} func c() {d()} func d() {e()} func e() {return} This is a simplified view,...
0
votes
0answers
32 views

How to unit test an SDL based frontend? Is it worth it? [duplicate]

I am currently beginning to create a small program/game in my free time and decided to try out SDL2 for my frontend/graphics code. I use the bindings from SDL2-CS, which is API-wise very close to the ...
1
vote
1answer
87 views

Testing Client-Side Code w/o Introducing Brittleness

I'm finding it tough to test my client-side code without testing implementation details. For example, lets say you are testing a login form that displays client-side validation errors and does not ...
-4
votes
2answers
186 views

How to unit test this converter?

I am beginning with unit testing in c#. Here is my converter: public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { int c = 0; if (value != null) {...
59
votes
7answers
12k views

Code coverage highlights unused methods - what should I do?

I have been tasked to increase code coverage of an existing Java project. I noticed that the code coverage tool (EclEmma) has highlighted some methods that are never called from anywhere. My initial ...
-2
votes
1answer
66 views

Is Python Doctest a White-Box or Black-Box technique

I have read many articles explaining the overview of black- and white-box testing, including this Stack Overflow question: https://stackoverflow.com/questions/7258524/unit-testing-black-box-testing-...
1
vote
4answers
263 views

How to prevent implementation/algorithm from leaking into unit tests?

I'm hesitating between algorithm and implementation as most of the questions here on how to prevent coupling between implementation and tests are about using spies/stubs or mocks. The typical problem ...
13
votes
4answers
840 views

How can you TDD for a bug that can only be tested after it has been fixed?

Here's one example: My web application contains draggable elements. When dragging an element, the browser produces a "ghost image". I want to remove the "ghost image" when dragging and I write a test ...
6
votes
2answers
191 views

What is the best practice of testing methods which call each other?

Say I have a method A which calls a method B and does one additional thing. The B method behaves differently in 10 different cases and I have a broad unit test describing it. And now I want to test my ...
90
votes
17answers
26k 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 ...
7
votes
5answers
710 views

With TDD, tests obviously fail first.But is that really true?

I have been taught that with TDD, the tests "naturally fail first but it is a good habit to run them anyway to see the red light". Well, but I am quite sure a unit test written first for a new feature ...
1
vote
1answer
502 views

How to pass a mock object into a class for unit tests

There seem to be many ways to pass a mocked object into a class for unit testing and I am unsure which is the proper approach to take for my PHP application. If I was using Dependency Injection then ...
3
votes
4answers
135 views

What is the best practice for a regression test that wants to know about encapsulated information?

Suppose you have two classes: Alpha, and Bravo. Alpha constructs a new Bravo class during its own constructor and exposes no public visibility to it. Bravo has a String named charlie (and a method to ...
0
votes
1answer
55 views

Refactoring code which uses a global list and updates list based on messages from an external system

I am refactoring some code where a globally defined list is updated based on messages from an external system. Currently there is a global instance of the list. Various messages update the list ...
1
vote
4answers
461 views

Are parsers a special case in unit testing?

Context While trying to get my parser classes under test, I noticed a common challenge for (unit-)testing them: They have only one public method, with a string as input and the parsed class as output....
-1
votes
1answer
39 views

Testing Application Helper that Depends on Controller

I am working on practicing TDD/BDD/ATDD and I am working on a rails application. I created a helper method that depends conditionally on the type of controller being tested. module ApplicationHelper ...
1
vote
0answers
64 views

Unit testing EventArgs - will there be timing issues?

I am writing a small game library. I capsule the logic in my model named Block. My model Block communicates only via events to it's clients (Is this a good thing to do!?). I wrote a simple unit test ...
1
vote
1answer
35 views

Worth it to test client side element presence for web application?

I've really been focusing on making my tests maintainable by focusing on BDD-style tests and only focusing on interfaces. I do not want my tests to be brittle and prevent refactoring. Is it worth it ...
1
vote
1answer
122 views

Fixtures, Tests, and Preconditions

When using factories, although more work is required up front, you know that you will be setting the correct pre-conditions for your test (up to what you have written.) However, when you use fixtures, ...
1
vote
2answers
162 views

When unit testing is not enough (what error cannot be found besides GUI)? [closed]

I am thinking, if I do proper testing for all methods in all my classes, then - disrgegarding UI - do I still need system testing? I mean, what would a simple example be, showing a case where unit ...
21
votes
5answers
4k views

Testing a list…All in the same test or one test for each condition?

I'm testing that a function does what expected on a list. So I want to test f(null) -> null f(empty) -> empty f(list with one element) -> list with one element f(list with 2+ elements) -> ...
1
vote
2answers
424 views

TDD workflow with integration testing

I'm having troubles with using the right workflow with TDD. Some people say we should design before writing any code, some say we should make a test, make it pass, then refactor the code and that ...
5
votes
3answers
2k views

What is the correct approach to test classes with inheritance?

Assuming I have the following (over-simplified) class structure: class Base { public: Base(int valueForFoo) : foo(valueForFoo) { }; virtual ~Base() = 0; int doThings() { return foo; }; ...
0
votes
3answers
162 views

How to design a unit test to safeguard against internal state dependency

I'm doing TDD. Let's say I expect some method m of some class C to call some function f (to keep things really simple). How would I design a unit test which would pick-up and therefore fail, if m ...
-1
votes
5answers
578 views

Is it good practice to have a unit test for a simple data method?

I have a simple data method that does this: public void Write(Foo foo) { db.Foos.Add(foo); db.SaveChanges(); } I was asked to write unit tests for this. To do so, I had to create a fake ...
82
votes
6answers
16k views

Is it bad practice to enforce an execution order for unit tests?

I am writing tests for a project that consists of multiple submodules. Each test case that I have written runs independent of each other and I clear all data between tests. Even though the tests run ...
5
votes
1answer
739 views

How to test interactors in clean architecture?

After reading the last book from Robert C. Martin, I've tried a to develop some big Go applications following clean architecture. While writing interactors, I end up with a lot of complex unit tests, ...
3
votes
1answer
140 views

Should BDD scenario be as minimal as possible, or is doing multiple cases in them okay?

Suppose were testing line segment intersections, and the intersections consider parallel and collinear lines to never intersect. Which one would be more natural to write? One case has more jammed ...
95
votes
11answers
16k views

How do you detect dependency problems with unit tests when you use mock objects?

You have a class X and you write some unit tests that verify behaviour X1. There's also class A which takes X as a dependency. When you write unit tests for A, you mock X. In other words, while unit ...
0
votes
2answers
185 views

How should I structure an automated test with a dependency on an HTTP endpoint?

I'm trying to better understand unit tests and integration tests. I understand unit tests should be in isolation and their dependencies should be mocked. But I'm slightly unclear on the best approach ...
37
votes
7answers
7k views

Is constructing objects with null parameters in unit tests OK?

I started writing some unit tests for my current project. I don't really have experience with it though. I first want to completely "get it", so I am currently using neither my IoC framework nor a ...
-6
votes
1answer
149 views

What is faster, to read 100MB from file or to compile 100MB of code?

I want to generate a test scene for a unit-test (big file parser). What will be faster, to have a test file and to perform I/O on it, or to generate a large memory buffer using some static parameters ...
6
votes
5answers
1k views

How to Unit-Test a parser of a file?

I'm implementing a metadata parser of image files from all formats. I want to write tests for it. One trivial way to do so is to have test image files of all formats as a resources for the tests, and ...
6
votes
6answers
1k views

Why wouldn't my team want to run unit tests automatically?

I'm managing a team in .net. They are writing unit tests, use them locally on a regular basis, and love it. However, they keep pushing to have the unit tests kept as a separate project and only want ...
1
vote
1answer
456 views

How to unit test database queries based on current date?

Is there a feasible way of testing SQL queries that contain things like CURRENT_DATE and NOW() in a unit test? For example: public function deletePastEntries(): bool { $sql = "DELETE FROM queue ...
2
votes
1answer
85 views

Keep unit tests common in separate library or duplicate?

Sometimes, ex. when using common mocks (same for data access layer as for presentation, I use often xunit ClassData), I come to the moment when I either copy common code to new unit test library or ...
7
votes
4answers
344 views

A few classes versus 20 classes? A question about bloat [closed]

In evaluating the work of an outsourcing company, I have found that they have a propensity for dividing work into as many classes as possible, each one being testable. In some ways this seems ...
3
votes
3answers
508 views

Should //given //when and //then comments be included in unit tests?

I've joined a new team of great colleagues and a difference of opinion has come up concerning including //given //when and //then comment blocks in unit tests. For example public void ...