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.
unit-testing
85,398
questions
3219
votes
59
answers
1.3m
views
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.
1227
votes
19
answers
927k
views
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?
1208
votes
11
answers
1.2m
views
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 ...
1019
votes
13
answers
835k
views
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");
1003
votes
27
answers
424k
views
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/
...
986
votes
16
answers
308k
views
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 ...
965
votes
25
answers
804k
views
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?
959
votes
31
answers
547k
views
Unit Testing C Code [closed]
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 ...
939
votes
7
answers
636k
views
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) {
...
819
votes
14
answers
675k
views
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:...
807
votes
17
answers
353k
views
What are the differences between unit tests, integration tests, smoke tests, and regression tests? [closed]
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 ...
803
votes
29
answers
216k
views
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 ...
758
votes
13
answers
602k
views
Difference between @Mock and @InjectMocks
What is the difference between @Mock and @InjectMocks in Mockito framework?
716
votes
13
answers
540k
views
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 ...
714
votes
30
answers
306k
views
What is a reasonable code coverage % for unit tests (and why)? [closed]
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 ...
713
votes
8
answers
356k
views
What is Mocking?
What is Mocking? .
708
votes
14
answers
485k
views
Run a single test method with maven
I know you can run all the tests in a certain class using:
mvn test -Dtest=classname
But I want to run an individual method and -Dtest=classname.methodname doesn't seem to work.
707
votes
10
answers
234k
views
What is the difference between 'it' and 'test' in Jest?
I have two tests in my test group. One of the tests use it and the other one uses test. Both of them seem to be working very similarly. What is the difference between them?
describe('updateAll', () =&...
663
votes
10
answers
235k
views
C# "internal" access modifier when doing unit testing
I'm trying to figure out if I should start using more of internal access modifier.
I know that if we use internal and set the assembly variable InternalsVisibleTo, we can test functions that we don't ...
661
votes
12
answers
552k
views
How to run only one unit test class using Gradle?
I am new to Gradle. I use Gradle 1.10 and Ubuntu 13.
I want to know if there's any command to execute only one unit test class, similar to testOnly in SBT.
647
votes
11
answers
508k
views
Unit test naming best practices [closed]
What are the best practices for naming unit test classes and test methods?
This was discussed on SO before, at What are some popular naming conventions for Unit Tests?
I don't know if this is a very ...
593
votes
6
answers
402k
views
Can Mockito capture arguments of a method called multiple times?
I have a method that gets called twice, and I want to capture the argument of the second method call.
Here's what I've tried:
ArgumentCaptor<Foo> firstFooCaptor = ArgumentCaptor.forClass(Foo....
572
votes
44
answers
311k
views
Is Unit Testing worth the effort? [closed]
I am working to integrate unit testing into the development process on the team I work on and there are some sceptics. What are some good ways to convince the sceptical developers on the team of the ...
570
votes
18
answers
134k
views
Where do the Python unit tests go? [closed]
If you're writing a library, or an app, where do the unit test files go?
It's nice to separate the test files from the main app code, but it's awkward to put them into a "tests" subdirectory inside ...
557
votes
7
answers
264k
views
Writing unit tests in Python: How do I start? [closed]
I completed my first proper project in Python and now my task is to write tests for it.
Since this is the first time I did a project, this is the first time I would be writing tests for it.
The ...
552
votes
23
answers
707k
views
How to test the type of a thrown exception in Jest
I'm working with some code where I need to test the type of an exception thrown by a function (is it TypeError, ReferenceError, etc.?).
My current testing framework is AVA and I can test it as a ...
549
votes
12
answers
448k
views
How do I print to console in pytest?
pytest will not print to the console when I use print. The documentation seems to say that it should work by default.
I am using pytest my_tests.py to run this test:
import myapplication as tum
class ...
520
votes
14
answers
209k
views
How to unit test abstract classes: extend with stubs?
I was wondering how to unit test abstract classes, and classes that extend abstract classes.
Should I test the abstract class by extending it, stubbing out the abstract methods, and then test all the ...
513
votes
11
answers
202k
views
Python unittest - opposite of assertRaises?
I want to write a test to establish that an Exception is not raised in a given circumstance.
It's straightforward to test if an Exception is raised ...
sInvalidPath=AlwaysSuppliesAnInvalidPath()
...
511
votes
32
answers
210k
views
How do you unit test private methods?
I'm building a class library that will have some public & private methods. I want to be able to unit test the private methods (mostly while developing, but also it could be useful for future ...
511
votes
22
answers
1.2m
views
Mocking static methods with Mockito
I've written a factory to produce java.sql.Connection objects:
public class MySQLDatabaseConnectionFactory implements DatabaseConnectionFactory {
@Override public Connection getConnection() {
...
492
votes
8
answers
962k
views
Mockito : how to verify method was called on an object created within a method?
I am new to Mockito.
Given the class below, how can I use Mockito to verify that someMethod was invoked exactly once after foo was invoked?
public class Foo
{
public void foo(){
Bar bar =...
480
votes
18
answers
415k
views
How do I run all Python unit tests in a directory?
I have a directory that contains my Python unit tests. Each unit test module is of the form test_*.py. I am attempting to make a file called all_test.py that will, you guessed it, run all files in the ...
477
votes
22
answers
491k
views
How to run test methods in specific order in JUnit4?
I want to execute test methods which are annotated by @Test in specific order.
For example:
public class MyTest {
@Test public void test1(){}
@Test public void test2(){}
}
I want to ensure ...
467
votes
14
answers
241k
views
What is the difference between unit tests and functional tests?
What is the difference between unit tests and functional tests? Can a unit test also test a function?
451
votes
5
answers
257k
views
How can I tell Moq to return a Task?
I've got an interface which declares
Task DoSomethingAsync();
I'm using MoqFramework for my tests:
[TestMethod()]
public async Task MyAsyncTest()
{
Mock<ISomeInterface> mock = new Mock<...
450
votes
20
answers
591k
views
How to test that no exception is thrown?
I know that one way to do it would be:
@Test
public void foo() {
try {
// execute code that you expect not to throw Exceptions.
} catch(Exception e) {
fail("Should not have ...
443
votes
9
answers
98k
views
What does “DAMP not DRY” mean when talking about unit tests?
I heard someone say that unit tests (e.g. nUnit, jUnit, xUnit) should be
DAMP not DRY
(E.g. unit tests should contain "damp code" not "dry code")
What are they talking about?
436
votes
5
answers
278k
views
What's the difference between unit tests and integration tests? [duplicate]
What's the difference between unit tests and integration tests?
Are there different names for these tests? Like some people calling unit tests functional tests, etc?
421
votes
19
answers
380k
views
Unit testing private methods in C#
Visual Studio allows unit testing of private methods via an automatically generated accessor class. I have written a test of a private method that compiles successfully, but it fails at runtime. A ...
418
votes
28
answers
390k
views
PATH issue with pytest 'ImportError: No module named ...'
I used easy_install to install pytest on a Mac and started writing tests for a project with a file structure likes so:
repo/
|--app.py
|--settings.py
|--models.py
|--tests/
|--...
417
votes
9
answers
168k
views
Different return values the first and second time with Moq
I have a test like this:
[TestCase("~/page/myaction")]
public void Page_With_Custom_Action(string path) {
// Arrange
var pathData = new Mock<IPathData>();
...
414
votes
7
answers
142k
views
NUnit vs. MbUnit vs. MSTest vs. xUnit.net [closed]
There are quite a lot of unittesting frameworks out there for .NET. I found this little feature comparison: http://xunit.github.io/docs/comparisons.html
Now I am to choose the best one for us. But how?...
413
votes
5
answers
144k
views
Conditionally ignoring tests in JUnit 4
OK, so the @Ignore annotation is good for marking that a test case shouldn't be run.
However, sometimes I want to ignore a test based on runtime information. An example might be if I have a ...
412
votes
31
answers
159k
views
Should I test private methods or only public ones? [closed]
I have read this post about how to test private methods. I usually do not test them, because I always thought it's faster to test only public methods that will be called from outside the object. Do ...
409
votes
7
answers
136k
views
What's the best strategy for unit-testing database-driven applications?
I work with a lot of web applications that are driven by databases of varying complexity on the backend. Typically, there's an ORM layer separate from the business and presentation logic. This makes ...
401
votes
9
answers
281k
views
Running a single test from unittest.TestCase via the command line
In our team, we define most test cases like this:
One "framework" class ourtcfw.py:
import unittest
class OurTcFw(unittest.TestCase):
def setUp:
# Something
# Other stuff ...
399
votes
10
answers
513k
views
How to run JUnit test cases from the command line
I would like to run JUnit test cases from the command line.
How can I do this?
398
votes
9
answers
226k
views
How to capture a list of specific type with mockito
Is there a way to capture a list of specific type using mockitos ArgumentCaptore. This doesn't work:
ArgumentCaptor<ArrayList<SomeType>> argument = ArgumentCaptor.forClass(ArrayList.class)...
379
votes
4
answers
201k
views
Code coverage with Mocha
I am using Mocha for testing my NodeJS application. I am not able to figure out how to use its code coverage feature. I tried googling it but did not find any proper tutorial. Please help.