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

CollectionAssert use with generics?

It appears that CollectionAssert cannot be used with generics. This is super frustrating; the code I want to test does use generics. What am I to do? Write boilerplate to convert between the two? Manually check collection equivalence? This…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
36
votes
6 answers

Chai unittesting - expect(42).to.be.an('integer')

According to http://chaijs.com/api/bdd/#a, a/an can be used to check for the type of a variable. .a(type) @param{ String } type @param{ String } message _optional_ The a and an assertions are aliases that can be used either as language chains or…
soerface
  • 6,417
  • 6
  • 29
  • 50
36
votes
3 answers

How do I mock User.Identity.GetUserId()?

I am trying to unit test my code which includes the line: UserLoginInfo userIdentity = UserManager.GetLogins(User.Identity.GetUserId()).FirstOrDefault(); I'm just stuck on one bit as I can't get: User.Identity.GetUserId() to return a value. I have…
AndrewPolland
  • 3,041
  • 4
  • 28
  • 39
36
votes
4 answers

Django test coverage vs code coverage

I've successfully installed and configured django-nose with coverage Problem is that if I just run coverage for ./manage.py shell and exit out of that shell - it shows me 37% code coverage. I fully understand that executed code doesn't mean tested…
Mikhail
  • 8,692
  • 8
  • 56
  • 82
36
votes
9 answers

Implementing Unit Testing with iOS

I've followed this tutorial to setup unit testing on my app when I got a little stuck. At bullet point 8 in that tutorial it shows this image, which is what I should be expecting when I build: alt text…
ingh.am
  • 25,981
  • 43
  • 130
  • 177
36
votes
6 answers

Unit testing device drivers

I have a situation where I need to write some unit tests for some device drivers for embedded hardware. The code is quite old and big and unfortunately doesn't have many tests. Right now, the only kind of testing that's possible is to completely…
Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
36
votes
13 answers

How to unit test a pseudo random number generator?

I have a pseudo random number generator (PRNG) class that I want to unit test. There are two approaches: write a test case that takes a large amount of samples and test whether they are properly distributed. This approach may lead to a fairly long…
andreas buykx
  • 12,608
  • 10
  • 62
  • 76
36
votes
10 answers

“File not found”, “linker command failed with exit code 1” in Xcode 4.5.1

Am developing an existing iOS application and I have to write unit test cases for this project. It is building and running in Simulator 6.0. Whenever I try to test the project, it is showing the error message below. Am not able to figure the exact…
Yuvaraj.M
  • 9,741
  • 16
  • 71
  • 100
36
votes
5 answers

Comparing Two objects using Assert.AreEqual()

I 'm writing test cases for the first time in visual studio c# i have a method that returns a list of objects and i want to compare it with another list of objects by using the Assert.AreEqual() method. I tried doing this but the assertion fails…
Vishweshwar Kapse
  • 921
  • 6
  • 23
  • 43
36
votes
5 answers

How do I test if a certain log message is logged in a Django test case?

I want to ensure that a certain condition in my code causes a log message to be written to the django log. How would I do this with the Django unit testing framework? Is there a place where I can check logged messages, similarly to how I can check…
Krystian Cybulski
  • 10,789
  • 12
  • 67
  • 98
36
votes
6 answers

How should I test a Future in Dart?

How can one test a method that returns Future before the test runner completes? I have a problem where my unit test runner completes before the asynchronous methods are completed.
adam-singer
  • 4,489
  • 4
  • 21
  • 25
36
votes
4 answers

Dependency injection with a static logger, static helper class

I have a static class which calls a static Logger class, e.g static class DoesStuffStatic { public static void DoStuff() { try { //something } catch(Exception e) { //do stuff; Logger.Log(e); } …
geejay
  • 5,440
  • 8
  • 50
  • 60
36
votes
7 answers

How to exclude certain tests in the Visual Studio Test Runner?

I have attributes on certain tests that I ideally don't want to run on every build. Most of my tests are normal unit tests and I do want them to run on every build. So: how can I exclude a test by category or project type? For example, I'd like to…
Fenton
  • 241,084
  • 71
  • 387
  • 401
36
votes
4 answers

Xcode 4.5 command line unit testing

Having an issue since updating to Xcode 4.5 when running my unit tests via command line. The following is the output i'm seeing when i try to run my tests Unknown Device Type. Using UIUserInterfaceIdiomPad based on screen size Terminating since…
Edward Huynh
  • 2,907
  • 1
  • 27
  • 26
36
votes
4 answers

Unit under test: Impl or Interface?

Suppose I have interface and implementation class that implements it and I want to write unit-test for this. What should I test interface or Impl? Here is an example: public interface HelloInterface { public void sayHello(); } public class…
alexsmail
  • 5,661
  • 7
  • 37
  • 57
1 2 3
99
100