Linked Questions

58
votes
5answers
9k views

Do I need unit test if I already have integration test?

If I already have integration test for my program, and they all passed, then I have a good feel that it will work. Then what are the reasons to write/add unit tests? Since I already have to write ...
18
votes
4answers
2k views

How to structure tests where one test is another test's setup?

I'm integration testing a system, by using only the public APIs. I have a test that looks something like this: def testAllTheThings(): email = create_random_email() password = ...
8
votes
3answers
27k views

Unit testing functions calling other tested functions [duplicate]

I have three functions: ValidateUsername(), which determines if a string is a valid username according to some rules SetUsername() which sets a string as a user's Username if it passes validation ...
4
votes
1answer
4k views

Java Unit testing multiple test cases in one test [duplicate]

Is it good practice to have multiple test cases in one test or should I always create one test for single test case in every situation even if it is redundant? Why?
3
votes
4answers
3k views

Implementing TDD for existing code [duplicate]

I've just been learning Unit Testing and I'm trying to understand how I could incorporate it with a project with existing code. Say I wanted to write tests for a specific class in that project, but ...
1
vote
1answer
1k views

Focus of unit tests on code [duplicate]

I discover mocking strategies after 50% of tests was writen, and I'm confused about how much focus unit tests should have, for example on class method. I know that for external dependencies you should ...
0
votes
1answer
160 views

Test logic with external data [duplicate]

I have a site programmed in PHP where I make more call to external server that return me big xml of data. For each record of the xml I have to make other external call to get other data. So my ...
1
vote
2answers
149 views

How to write unit tests a method with a result that is highly based on another method [duplicate]

How to write unit tests a method with a result that is highly based on another method ? async function getStatus( session, correlationId, data ) { const client = new SomeAPIWrapperClient({ ...
0
votes
1answer
78 views

Do we do white box testing on methods or on an overall program? [duplicate]

I am very confused about white box testing. A simplified version of the example: the entire system consists of three methods - methodA(), methodB(), methodC(). The program starts from methodA(), and ...
82
votes
11answers
11k views

Shouldn't unit tests use my own methods?

Today I was watching a "JUnit basics" video and the author said that when testing a given method in your program, you shouldn't use other of your own methods in the process. To be more specific, he ...
13
votes
5answers
11k views

How to make one test depend on the results of another test?

Let's say there is a utility class that provides some common static methods used everywhere in your code by many other classes. How would you design your unit tests for the consumers of the utility ...
15
votes
4answers
2k views

Is there any value in writing a unit test that is a subset of another test?

To give a slightly contrived example, let's say I want to test that a function returns two numbers, and that the first one is smaller than the second one: def test_length(): result = my_function()...
7
votes
4answers
756 views

Should I test a method that calls a method that is already tested?

I have a method that does something like this public void addFunds(Account account, int price) { int credits = account.getCredits() account.setCredits(credits + price) saveOrUpdate(account) }...
8
votes
2answers
866 views

Philosophy on unit testing daisy-chained methods?

Problem Statement: I have a class that does some validation and it looks something like this: func a() {b()} func b() {c()} func c() {d()} func d() {e()} func e() {return} This is a simplified view,...
3
votes
5answers
1k views

How do you unit test a function that clears properties?

I have a very common function that I have always unit tested in the same way, but I'm wondering if there is a better solution or if it's even possible a code smell is involved. It seems like a very ...
7
votes
3answers
873 views

Should unit tests always overlap

This is a test design question. I have a class 'handler' that accepts a 'validator' that checks some business logic on what's passed to the handler. I made the unit tests for the validator and now I'...
6
votes
2answers
215 views

What is the best practice of testing methods which call each other?

Say I have a method A which calls a method B and does one additional thing. The B method behaves differently in 10 different cases and I have a broad unit test describing it. And now I want to test my ...
4
votes
3answers
248 views

How narrow should my unit tests be?

Here's an example: I have a chat module in my app, and there's a ChatService class that is responsible for networking, and there's a ChatNotificationService helper class that is responsible for ...
2
votes
2answers
142 views

Should I write duplicated tests for 'setDate' and 'isValidDate' methods?

Let's say I have a method called setDate. I also have another method called isValidDate to see if a string is a valid date string. For convenience, setDate uses isValidDate internally as a mean of ...
1
vote
2answers
197 views

One method per unit test?

Say I have a Service Layer method like this: public void TestMethod() { DomainObject domainObject = new DomainObject(); domainObject.TestMethod(); } TestMethod looks like this in the Domain ...
0
votes
1answer
331 views

Bad idea to use the same test cases for multiple parts of a contiguous algorithm?

I'm working on an algorithm that's split up into multiple parts, each of which performs different tasks but is more or less separate from the others, by which I mean the only interaction between the ...
-1
votes
1answer
134 views

Best practices on unit tests for consecutive functions

Let's say we have function A and function B which perform consecutive operations on some data, with B never receiving the data before A processes it. Function A makes exhaustive checks on potential ...
-2
votes
2answers
105 views

Should you write integration tests at every level?

Suppose I have some code as such: function1() { function2() } function2() { function3() } function3() { function4() } function4() { ... } Do I have to write integration tests for ...
4
votes
0answers
154 views

How to manage success dependency between unit tests [duplicate]

How do you manage the dependency of one unit test on another unit test succeeding? So, supposed I have a class and this class has say 5 methods. I create like 2 dozen unit tests (test methods) for ...
0
votes
3answers
241 views

Unit Testing method with multiple steps

I have a validation method like this def validate(a, b, c, d, e, f): Boolean = { val rs1 = check1(a, b) val rs2 = check2(c, d) val rs3 = check3(e, f) rs1 && rs2 && rs3 } I ...
-1
votes
4answers
107 views

How do I test a method which requires another method of the same class to be invoked first?

I have a simple class which looks like this: class SpecialList { private List<Integer> varList; SpecialList() { varList = new ArrayList<>(); } void addVar(int i) {...