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.

From Wikipedia:

Unit testing is a method by which individual units of source code are tested to determine if they are fit for use. Intuitively, one can view a unit as the smallest testable part of an application. In procedural programming a unit could be an entire module but is more commonly an individual function or procedure. In object-oriented programming a unit is often an entire interface, such as a class, but could be an individual method. Unit tests are created by programmers or occasionally by white box testers during the development process.

Ideally, each test case is independent from the others: substitutes like method stubs, mock objects, fakes and test harnesses can be used to assist testing a module in isolation. For example, these substitutes also known as Test Doubles can be used to isolate dependencies such as Databases and File System.

Unit tests are typically written and run by software developers to ensure that code meets its design and behaves as intended.Wikipedia.

Unit testing is closely related to Test Driven Development.

Benefits

The goal of unit testing is to isolate each part of the program and show that the individual parts are correct. A unit test provides a strict, written contract that the piece of code must satisfy. As a result, it affords several benefits.

  1. Finds problems early
  2. Facilitates change
  3. Simplifies integration
  4. Documentation
  5. Design

Books

There are a lot of books about unit test in general and about specific frameworks to specific languages, some examples are:

External links

List of unit testing frameworks

84762 questions
36
votes
3 answers

Debugging Go tests in Visual Studio Code

On my Windows machine, I have Visual Studio Code installed. To run tests manually, I go in console to projects folder and enter go test main_test.go It works perfectly. But I have a situation in which I need to debug my test to understand what's…
Vitalii
  • 10,091
  • 18
  • 83
  • 151
36
votes
14 answers

Django: Best way to unit-test an abstract model

I need to write some unit tests for an abstract base model, that provides some basic functionality that should be used by other apps. It would be necessary to define a model that inherits from it just for testing purposes. Are there any…
Bernhard Vallant
  • 49,468
  • 20
  • 120
  • 148
36
votes
3 answers

Unable to simulate keypress event in Angular 2 unit test (Jasmine)

I am using a directive to get the data from input used as a filter text. here is my hostlistener in the directive: @HostListener('input', ['$event.target.value']) public onChangeFilter(event: any): void { console.log('input event fired, value:…
Akanksha Gaur
  • 2,636
  • 3
  • 26
  • 50
36
votes
8 answers

changing order of unit tests in Python

How can I make it so unit tests in Python (using unittest) are run in the order in which they are specified in the file?
user248237
36
votes
10 answers

Unit testing a Hibernate driven application?

This may be a naive question, but I am new to both the junit and hibernate frameworks and I was wondering what the best way to go about unit testing an application that is largely calls to hibernate, or if it is even necessary to do so? What is the…
James McMahon
  • 48,506
  • 64
  • 207
  • 283
36
votes
3 answers

Unit Test Laravel's FormRequest

I am trying to unit test various custom FormRequest inputs. I found solutions that: Suggest using the $this->call(…) method and assert the response with the expected value (link to answer). This is overkill, because it creates a direct dependency…
Dov Benyomin Sohacheski
  • 7,133
  • 7
  • 38
  • 64
36
votes
5 answers

Can I run a single unit test from the command line for a Grails project?

I've been running my Grails unit tests by typing grails test-app :unit, which runs all unit tests. Is there a way to specify a single test? Edit: So far, everyone is basically saying the same thing, but when I do that, no tests are run. Any more…
Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
36
votes
11 answers

Xcode 7 Tests don't run but reports success

I'm trying to add a test target to my project, however When I run the tests it seems like the actual tests aren't being executed, instead Xcode reports success always, but the small square which indicates if the test passed or not remains clear. I…
Emanuel
  • 1,981
  • 1
  • 20
  • 28
36
votes
5 answers

How do you unit test an interface?

For example, there is a interface IMyInterface, and three classes support this interface: class A : IMyInterface { } class B : IMyInterface { } class C : IMyInterface { } In the simplest way, I could write three test class : ATest, BTest, CTest…
Chetan
  • 46,743
  • 31
  • 106
  • 145
36
votes
5 answers

java.lang.IllegalStateException: missing behavior definition for the preceding method call getMessage("title")

I'm using EasyMock(version 2.4) and TestNG for writing UnitTest. I have a following scenario and I cannot change the way class hierarchy is defined. I'm testing ClassB which is extending ClassA. ClassB look like this public class ClassB extends…
user362199
  • 361
  • 1
  • 3
  • 3
36
votes
1 answer

Unit test and private vars

I'm writing a BDD unit test for a public method. The method changes a private property (private var) so I'd like to write an expect() and ensure it's being set correctly. Since it's private, I can't work out how access it from the unit test…
Robert Gummesson
  • 5,630
  • 2
  • 17
  • 16
36
votes
3 answers

Boost.Test: Looking for a working non-Trivial Test Suite Example / Tutorial

The Boost.Test documentation and examples don't really seem to contain any non-trivial examples and so far the two tutorials I've found here and here while helpful are both fairly basic. I would like to have a master test suite for the entire…
Robert S. Barnes
  • 39,711
  • 30
  • 131
  • 179
36
votes
9 answers

NUnit, is it possible to continue executing test after Assert fails?

In a test that contains some asserts, for example: Assert.AreEqual(1,1); Assert.AreEqual(2,1); Assert.AreEqual(2,2); is it possible to let the test keep running after it fails at some point? In the example, first condition is true, second fails and…
ccalboni
  • 12,120
  • 5
  • 30
  • 38
36
votes
1 answer

Rhino Mocks - Difference between GenerateStub & GenerateMock

Can any of the Rhino experts explain me by giving a suitable example of the difference between the above methods on the MockRepository class (Rhino Mocks framework). Where should one use Stub over Mock method or otherwise?
chugh97
  • 9,602
  • 25
  • 89
  • 136
36
votes
5 answers

ASP.NET MVC unit test controller with HttpContext

I am trying to write a unit test for my one controller to verify if a view was returned properly, but this controller has a basecontroller that accesses the HttpContext.Current.Session. Everytime I create a new instance of my controller is calls…
amurra
  • 15,221
  • 4
  • 70
  • 87