Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

2
votes
2answers
163 views

Should my classes have separate constructors just for unit testing?

I like to write classes with two constructors: a primary constructor used in production code, and a default constructor just for unit tests. I do this because the the primary constructor creates other ...
20
votes
6answers
9k views

How should I test my TEST code?

One of the few things that most software developers agree on is that you shouldn't rely on code to work correctly unless you test it. If you don't test it, it may have hidden bugs that are only ...
0
votes
2answers
53 views

Unit and integration testing of microservice architecture

I am investigating how to test a project. Some information about the project: Microservice architecture, with roughly 20 services. About 10 of them with a separate database. We use ServiceFabric ...
0
votes
1answer
62 views

Unit testing for a method in a class which uses constructor DI (prism)

I have a class that uses constructor DI for IEventAggregator public SomeViewModel(IEventAggregator eventAggregator) { this.eventAggregator = eventAggregator; ...
26
votes
6answers
11k views

Should unit test expected results be hardcoded?

Should the expected results of a unit test be hardcoded, or can they be dependant on initialised variables? Do hardcoded or calculated results increase the risk of introducing errors in the unit test?...
346
votes
15answers
226k views

Is it OK to have multiple asserts in a single unit test?

In the comment to this great post, Roy Osherove mentioned the OAPT project that is designed to run each assert in a single test. The following is written on the project's home page: Proper unit ...
2
votes
4answers
123 views

Testing database insertion, removal, and modification from program code (i.e., rather than test code)

I want to ensure that my program code is correctly inserting, removing, and modifying data in my database. Full integration testing can be slow, and I have concerns about keeping mock objects and data ...
5
votes
5answers
6k views

Is it feasible and useful to auto-generate some code of unit tests?

Earlier today I have come up with an idea, based upon a particular real use case, which I would want to have checked for feasability and usefulness. This question will feature a fair chunk of Java ...
0
votes
2answers
47 views

How to name and organize unit tests with combinations of multiple parameters?

I am new to unit testing, but finally getting started. I have been running into a situation where my unit test names apparently grow too long to readable due to the multiple parameters and ...
-3
votes
0answers
36 views

Is there a term for a catalog of fakes, mocks and stubs that are preconfigured?

Let's say I have a user object... and there are a certain subsection of tests that always for example, want an "admin user" and others want "moderation users" ... or we want users with unicode names ...
-2
votes
1answer
65 views

Unit Tests - correct approach to test system with multiple layers

I'm wondering about Unit Tests. Let say i got a code ( in C#, but language is not important here): public class SOT: ISOT { List<string> _internalCollection = new List<string>(); ...
-2
votes
1answer
56 views

What is a common name for a module being tested?

When writing unit tests, I want to reduce the cognitive load of the reader as much as possible. One thing I've noticed that bothers me is that the variable names of the thing that is being tested are ...
-2
votes
1answer
58 views

How to identify test cases?

I wrote a small INI file parser as a library which I want to use in a bigger project. Following good practice I decided I should write test cases, too. But I fail to find a good start. The library is ...
4
votes
1answer
171 views

Boundaries of unit testing between my code and a library or sub-component

Background: I am new to testing in general, and have been studying it in context of JavaScript, specifically React.js, front-end development (actually new to this as well). For the question, I have ...
3
votes
1answer
691 views

What is a good method of storing test data for development and unit testing with angularjs and jasmine?

What is a good method of storing JSON data/objects for development and unit testing with angular and jasmine? I have seen some ways of accomplishing this. However, since I am new to unit testing with ...
16
votes
7answers
4k views

Is it a good idea to write all possible test cases after transforming the team to TDD to achieve a full coverage?

Assume we have a large enterprise-level application without any unit/functional tests. There was no test-driven development process during the development due to very tight deadlines (I know we should ...
0
votes
2answers
84 views

Should linters no-magic-numbers rule used in testing?

So I am just starting to use a linter and I like the no-magic-numbers rule in generally. But in unit test files it is giving me 1000 warnings. Here is what they look like: it('getWeekNumber', ...
-1
votes
1answer
33 views

Where do you put tests that are not unit tests in a Maven project?

I'm building a Spark-based, text analysis package using both Java and Scala. I have a series of transform functions, which take in one dataframe and spit out another, and that can be chained together ...
5
votes
2answers
141 views

How to track C++ class traits?

As a fan of regular types and value semantics, I'm keen on classes becoming more regular and being non-polymorphic. As a fan of non-throwing operations, I'm keen on operations being noexcept. I also ...
27
votes
2answers
3k views

Is a Unit test considered brittle if it fails when the business logic changes?

Please see the code below; it tests to see if a person with Gender of female is eligible for offer1: [Fact] public void ReturnsFalseWhenGivenAPersonWithAGenderOfFemale() { var personId = Guid....
0
votes
3answers
79 views

How to structure a set of classes that deal with external APIs for maximum testability?

I'm developing a set of classes designed to communicate with external APIs, and I'm running into trouble with how to properly structure everything for proper loose coupling and unit testing. ...
7
votes
8answers
327 views

Unit test which asserts that current thread is the main thread

The question is does it make sense to write a unit test which asserts that the current thread is the main thread? Pros/cons? Recently I've seen the unit test which asserts the current thread for the ...
3
votes
3answers
139 views

refactoring function to have a robust design

i am having a simple app example here: say i have this piece of code which handles requests from user to get a list of books stored in a database. from .handlers import all_books from flask import ...
18
votes
7answers
3k views

How do you unit-test code using graph structures?

I am writing (recursive) code that is navigating a dependency graph looks for cycles or contradictions in the dependencies. However, I am not sure how to approach unit testing this. The problem is ...
0
votes
1answer
63 views

How to test an API that can change the response during time?

I currently use the Google trends API and I implemented a class to integrate with its services. As you probably know, Google trends tracks statistics which will change over time. So, when I write ...
8
votes
4answers
976 views

How should I unit-test a class that relies on having realistic data?

I have a class that encapsulates the results of a scientific measurement. I’m building in unit tests from the beginning, but I don’t have a lot of experience with unit testing and I’m not sure which ...
7
votes
4answers
321 views

Should log statements be tested?

When writing tests I usually ignored logging statements, but now I wonder if it was right. The logs are often important tools when diagnosing production issues, moreover there can even be ...
1
vote
1answer
79 views

How to organize my test functions?

I'm writing a software application of a few thousand lines of code (in Python), and in order to keep the whole thing together, slowly but certainly the need for unit tests (and later, other types of ...
0
votes
1answer
67 views

Testing - concise mocks vs asserting received calls

Let's say we have this method: string Add(int a, int b) { var sum = _calculator.Add(a,b); var response = _formatter.Format(sum); return response; } Now I want to unit-test this method. ...
3
votes
2answers
593 views

How to write good javascript unit test descriptions

I come from a python background. I typically write long unit test descriptions that link the test to a user story. The test is as much about justifying why the code exists and how it fits into the ...
3
votes
1answer
157 views

Dealing with conditional verification logic using guard assertion

I have parametrized test with 2 variants: NULL value and any NOT NULL value From this value is depends one assert: In case NULL it should be checked if object has field1 with value1 In case ...
2
votes
2answers
159 views

What is recommended way to create test data for unit test cases?

I am new to TDD/unit testing. I am going to write a complex scheduling algorithm in Java. As this module is a core part of our application and there are number of scenarios in it, I want to write ...
6
votes
5answers
635 views

How far should I go with dependency injection and mocking?

Say I have a following class: public class A { public void execute() { if (something) ThirdPartyApi.method(); } } Now, I would like to test in particular the execute() method. I ...
0
votes
1answer
131 views

Unit testing protected methods which have no reason to be public

I need to make a sponsorship system with complex business requirements. Basically, after a user makes a payment, the system should get triggered. There are many different types of sponsoring, so I ...
2
votes
1answer
47 views

MVP Passive View -> Dependency Injection -> Factory Pattern -> Is testing behaviour whilst ignoring state enough?

I am writing an application using the Model/View/Presenter passive view pattern. I have view that contains multiple items in a list. My model is of type AudioDrama. For filtering that list I have ...
9
votes
4answers
7k views

Writing Unit Tests for a Class That Starts External EXE

I wrote a C# class that is used to start a list of EXEs (not one of mine - third party EXEs I have to run) and keep them running (will check occasionally to make sure it is still running, and start ...
1
vote
1answer
99 views

What's the purpose of xfail tests?

Some test frameworks, notably DejaGnu and py.test, have an "XFAIL" flag/status for a test which says that this test is expected to fail. If the test succeeds, it's marked as "XPASS" (unexpected pass). ...
2
votes
3answers
763 views

How much should I break up my unit tests?

I recently learned about unit tests and TDD and have started going back to add unit tests to my code (and thank god, it's proven that some things I've written are much more broken than I thought). ...
28
votes
5answers
13k views

Should you hard code your data across all unit tests?

Most unit testing tutorials/examples out there usually involve defining the data to be tested for each individual test. I guess this is part of the "everything should be tested in isolation" theory. ...
4
votes
2answers
623 views

Mocking a utilities module for unit testing? [duplicate]

I'm writing unit tests for a class that makes use of a general utilities module. In general I want to mock all the dependencies of the class, but mocking some of the utility functions feels weird. ...
4
votes
2answers
214 views

How to engineer and test mission critical software?

TL;DR: My goal is to create some framework with which I could discover/test all possible internal states of my application so that I would approach with confidence for example engineering software for ...
1
vote
2answers
112 views

Peer review of unit test

What is a good strategy for Q/A Engineers peer reviewing Unit tests as part of the software development life cycle? I think that there are, indeed, things that I should check regarding the unit test ...
64
votes
6answers
10k views

How exactly should unit tests be written without mocking extensively?

As I understand, the point of unit tests is to test units of code in isolation. This means, that: They should not break by any unrelated code change elsewhere in the codebase. Only one unit test ...
34
votes
9answers
5k views

Is it reasonable to not write unit tests because they tend to get commented out later or because integration tests are more valuable?

I was discussing unit/integration testing with a colleague, and he made an interesting case against writing unit tests. I'm a big unit test (JUnit primarily) proponent, but am interested to hear ...
0
votes
1answer
72 views

How to write unit tests for my project

I'm relatively new to programming and have never written tests. I want to write unit tests for a group project (Angular web app with node background environment written in typescript), which you can ...
4
votes
3answers
2k views

Integration Testing: Testing Service to Service

I'm looking for some advice on testing strategies for service to service communication. I have one service (service A) that makes a call to another service (B) - which is a rest API. Both services ...
-1
votes
1answer
93 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 ...
0
votes
2answers
150 views

Naming conventions for .net C# Unit test projects for better sorting [closed]

I currently use the following naming scheme for my unit test projects.. if I have a project "MyApp", I will have.. -- MyApp -- MyApp.Tests I see this is quite a common practice. Now, the problem is,...
1
vote
4answers
3k views

Should I use virtual methods to make mocking possible?

So far I have used to write interfaces to those classes I want to mock/fake. Reason for that is that those classes don't have any virtual method to overwrite. But resently I have figured out that I ...
2
votes
1answer
148 views

Unit Testability of Builder pattern

I'm looking at the builder pattern for helping setting up dependencies and parameters that may require complex logic for a class. But from the examples I've seen, the builder pattern does not seem ...