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
35
votes
3 answers

How do I unit test an __init__() method of a python class with assertRaises()?

I have a class: class MyClass: def __init__(self, foo): if foo != 1: raise Error("foo is not equal to 1!") and a unit test that is supposed to make sure the incorrect arg passed to the constructor properly raises an error: def…
Sam McAfee
  • 10,057
  • 15
  • 60
  • 64
35
votes
3 answers

How not to build before executing unit tests in Visual Studio 2010

Whenever I run unit tests, Visual Studio builds to make dlls and exes even when I modified nothing in code. How can I make so that Visual Studio doesn't build when I didn't make any changes before running the unit tests?
prosseek
  • 182,215
  • 215
  • 566
  • 871
35
votes
15 answers

What do you use to Unit-Test your Web UI?

The company I'm currently working for is using Selenium for Uniting-Testing our User Interface. What do you use to Unit-Test your Web UI and how effective do you find it?
Eldila
  • 15,426
  • 23
  • 58
  • 62
35
votes
6 answers

NestJS - Test suite failed to run Cannot find module 'src/article/article.entity' from 'comment/comment.entity.ts'

i need help with nestjs and jest testing. I am new to NestJS and i got stuck on Cannot find module error when i run tests. I am trying to test my service and when i run tests i have received error message: src/article/article.service.spec.ts ● Test…
Ergeros
  • 361
  • 1
  • 3
  • 6
35
votes
1 answer

How to mock just one static method in a class using Mockito?

The following line seems to mock all static methods in the class: MockedStatic sampleMock = Mockito.mockStatic( Sample.class ); sampleMock.when( () -> Sample.sampleStaticMethod( Mockito.any( String.class ) ) ).thenReturn( "response" ); Is…
Kumar
  • 1,023
  • 1
  • 10
  • 23
35
votes
5 answers

Unable to Mock HttpClient PostAsync() in unit tests

I am writing test cases using xUnit and Moq. I am trying to mock PostAsync() of HttpClient, but I get an error. Below is the code used for mocking: public TestADLS_Operations() { var mockClient = new Mock(); …
chandra sekhar
  • 1,093
  • 4
  • 14
  • 27
35
votes
5 answers

Nullinjectorerror: no provider for FormBuilder (I'm importing ReactiveFormsModule)

So I'm trying to write a really basic test for a component. I just want to make sure that the form that I'm creating using FormBuilder is an instance of FormGroup, but I'm consistently getting a NullInjectorerror: No provider for FormBuilder, and…
Sophie McCall
  • 361
  • 1
  • 3
  • 4
35
votes
5 answers

Jest Error: 'jest' is not recognized as an internal or external command, operable program or batch file

I have already installed Jest in my React project. I even installed jest-cli, but still can't use Jest in the command line. I can do an npm test, as it is in my package.json, but I have problems with the test, and in order to fix it I need to access…
Gergan Zhekov
  • 944
  • 1
  • 8
  • 27
35
votes
4 answers

How can I get Eclipse 2018.09 to use the JUnit 4 test runner by default?

I've just upgraded to Eclipse 2018.09 (the problem also occurs on Eclipse Photon), but it doesn't play nicely with a project that uses JUnit 4. The new version appears to run all tests using a JUnit 5 runner by default, which fails with the…
Kaypro II
  • 3,210
  • 8
  • 30
  • 41
35
votes
1 answer

Jest: How to correctly mock a node module?

I want to mock the node_module 'React Native Keychain' in React Native with Jest. Following the docs I created a folder called __mocks__ and created a file called react-native-keychain.js in it. Here is the code within the file: export default…
J. Hesters
  • 13,117
  • 31
  • 133
  • 249
35
votes
2 answers

How to mock/spy an imported function in Angular unit testing

Let's say i have an angular 6 component with a method test which returns some value: import { doSomething } from './helper'; @Component({ ... }) export class AppComponent { test() { const data = doSomething(1); return…
user7995820
  • 412
  • 1
  • 4
  • 10
35
votes
1 answer

how to export (JUnit) test suite as executable jar

Is there a way in eclipse (Helios) to package/export my JUnit test suites (or maybe even test cases if possible) as executable jars? I know how to generate runnable jars from projects with a main class, but i'm clueless about how to include a…
kostja
  • 60,521
  • 48
  • 179
  • 224
35
votes
3 answers

Angular - unit test for a subscribe function in a component

Angular 4 unit test for a subscribe. I want to test that my subscribe returns an array of Users. I want to mock a list of users and test a function called getUsers. The subscribe unit test doesnt work. Something wrong with the syntax. This is my…
AngularM
  • 15,982
  • 28
  • 94
  • 169
35
votes
10 answers

How do I provide ILogger in my unit tests of .NET Core code?

Given a class with a constructor signature of public Foo(ILogger logger) { // ... } that I want to test, I need some way to provide an ILogger in the test. It's been asked before, but the only answer then was to set up a full-blown…
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
35
votes
4 answers

How to test if function was called with defined parameters ( toHaveBeenCalledWith ) with Jest

I want to test, if particular function was called in my test and with the correct parameters. From JEST documentation I'm not able to figure out, what is the correct way to do it. Let's say I have something like this: // add.js function child(ch)…
DHlavaty
  • 12,958
  • 4
  • 20
  • 25
1 2 3
99
100