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
3189
votes
59 answers

How do I test a class that has private methods, fields or inner classes?

How do I use JUnit to test a class that has internal private methods, fields or nested classes? It seems bad to change the access modifier for a method just to be able to run a test.
MattGrommes
  • 11,974
  • 9
  • 37
  • 40
1190
votes
19 answers

How do you test that a Python function throws an exception?

How does one write a unit test that fails only if a function doesn't throw an expected exception?
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173
1181
votes
11 answers

How to mock void methods with Mockito

How to mock methods with void return type? I implemented an observer pattern but I can't mock it with Mockito because I don't know how. And I tried to find an example on the Internet but didn't succeed. My class looks like this: public class World…
ibrahimyilmaz
  • 18,331
  • 13
  • 61
  • 80
994
votes
14 answers

How do I generate a stream from a string?

I need to write a unit test for a method that takes a stream which comes from a text file. I would like to do do something like this: Stream s = GenerateStreamFromString("a,b \n c,d");
Omu
  • 69,856
  • 92
  • 277
  • 407
982
votes
27 answers

Running unittest with typical test directory structure

The very common directory structure for even a simple Python module seems to be to separate the unit tests into their own test directory: new_project/ antigravity/ antigravity.py test/ test_antigravity.py setup.py …
Major Major
  • 10,286
  • 4
  • 19
  • 9
964
votes
16 answers

What's the difference between faking, mocking, and stubbing?

I know how I use these terms, but I'm wondering if there are accepted definitions for faking, mocking, and stubbing for unit tests? How do you define these for your tests? Describe situations where you might use each. Here is how I use them: Fake:…
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
957
votes
25 answers

How do I use Assert to verify that an exception has been thrown with MSTest?

How do I use Assert (or other Test class) to verify that an exception has been thrown when using MSTest/Microsoft.VisualStudio.TestTools.UnitTesting?
Alex
  • 75,813
  • 86
  • 255
  • 348
952
votes
31 answers

Unit Testing C Code

I worked on an embedded system this summer written in straight C. It was an existing project that the company I work for had taken over. I have become quite accustomed to writing unit tests in Java using JUnit but was at a loss as to the best way…
Paul Osborne
  • 5,024
  • 6
  • 24
  • 20
906
votes
7 answers

How to verify that a specific method was not called using Mockito?

How to verify that a method is not called on an object's dependency? For example: public interface Dependency { void someMethod(); } public class Foo { public bar(final Dependency d) { ... } } With the Foo test: public class…
beluchin
  • 12,047
  • 4
  • 24
  • 35
803
votes
17 answers

What are the differences between unit tests, integration tests, smoke tests, and regression tests?

What are unit tests, integration tests, smoke tests, and regression tests? What are the differences between them and which tools can I use for each of them? For example, I use JUnit and NUnit for unit testing and integration testing. Are there any…
caltuntas
  • 10,747
  • 7
  • 34
  • 39
796
votes
29 answers

How should I unit test multithreaded code?

I have thus far avoided the nightmare that is testing multi-threaded code since it just seems like too much of a minefield. I'd like to ask how people have gone about testing code that relies on threads for successful execution, or just how people…
jkp
  • 78,960
  • 28
  • 103
  • 104
750
votes
14 answers

How do I properly assert that an exception gets raised in pytest?

Code: # coding=utf-8 import pytest def whatever(): return 9/0 def test_whatever(): try: whatever() except ZeroDivisionError as exc: pytest.fail(exc, pytrace=True) Output: ================================ test session…
Gill Bates
  • 14,330
  • 23
  • 70
  • 138
730
votes
13 answers

Difference between @Mock and @InjectMocks

What is the difference between @Mock and @InjectMocks in Mockito framework?
user2249972
  • 7,321
  • 3
  • 13
  • 5
716
votes
13 answers

JavaScript unit test tools for TDD

I've looked into and considered many JavaScript unit tests and testing tools, but have been unable to find a suitable option to remain fully TDD compliant. So, is there a JavaScript unit test tool that is fully TDD compliant?
Mark Levison
  • 788
  • 3
  • 9
  • 22
703
votes
30 answers

What is a reasonable code coverage % for unit tests (and why)?

If you were to mandate a minimum percentage code-coverage for unit tests, perhaps even as a requirement for committing to a repository, what would it be? Please explain how you arrived at your answer (since if all you did was pick a number, then I…
sanity
  • 35,347
  • 40
  • 135
  • 226
1
2 3
99 100