Skip to main content

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.

Filter by
Sorted by
Tagged with
0 votes
3 answers
124 views

.Net 8 XUnit: How to mock MySql in a CQRS API using integration tests?

This is a rewrite of my own .Net 8 XUnit: Should my tests use real data or how to mock MySql with CQRS? in a much more clear way: The reason for rewriting my previous question is because in the ...
3 votes
2 answers
340 views

.Net 8 XUnit: Should my tests use real data or how to mock MySql with CQRS?

I'm creating tests for our .Net 8 API. We use MySql for data storage and CQRS pattern for queries. In my personal opinion, I shouldn't use the real database for testing, because you will use an ...
2 votes
2 answers
311 views

In unit testing: How to abstract a dependency of subject under test?

Disclaimer: I am learning unit testing. I am also kind of beginner in object-oriented design. Currently, I am involved in the development of an application to manage the finance of a humble food ...
76 votes
6 answers
15k 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 ...
14 votes
4 answers
2k views

Do mocks violate the Open/Closed principle?

Some time ago I read, on a Stack Overflow answer that I can't find, a sentence that explained that you should test public APIs, and the author said that you should test interfaces. The author also ...
2 votes
1 answer
694 views

Approach to software testing with docker

When discussing the testing approach, we had disagreements. We develop software that we package into an image and distribute. We have two suggestions for testing: Build a separate image with a test ...
5 votes
4 answers
1k views

With Kent Beck's "test && commit || revert" (TCR), do you skip the "red" of "red, green, refactor"?

If you haven't heard of Kent Beck's TCR, it can be summarized with this: any time your tests go green, you commit; anytime your tests go red, you git reset --hard. This post is about how to practice ...
2 votes
3 answers
170 views

How to decouple spagheti code for unit tests [duplicate]

A little background on the project: we as a company receive a spaghetti source code, and into that we add even more spaghetti code. So with that I want to say that complete restructuring and ...
0 votes
2 answers
266 views

"Syncing" unit-tests for different layers

Consider unit tests for two (or more) consequent layers in web-application, backend, eg views (these are concerned with parsing form parameters, rendering the response) and actions (application logic)....
143 votes
14 answers
24k views

Time difference between developing with unit tests vs no tests

I'm a solo developer with a pretty time-constrained work environment where development time ranges usually from 1-4 weeks per project, depending on either requirements, urgency, or both. At any given ...
13 votes
9 answers
10k views

How long should we generally spend writing unit tests for a new feature or bug fixing?

When I have to implement a new feature or fix a bug, I usually try to recreate the situation with a test. I sometime spend around 3 hours coming up with fixtures and writing the test. The actual ...
1 vote
2 answers
861 views

Entity Framework and Domain Driven Design Testability

I'm using a DDD approach for the Domain classes. Although, I have a problem on my design, that I'm handling it now but didn't have a good idea to over tackle it. My Architecture is the follow one: - ...
-4 votes
1 answer
92 views

Refactoring the testing code inside a big switch code block

The main logic of my function is a switch case, with the default case being the abnormal case. function main(input) { let result = process(input) switch (result): { //normal cases case ...
6 votes
1 answer
792 views

Cons of using unconstrained frameworks for unit testing

It's very easy to write unit tests for legacy code using unconstrained frameworks, such as TypeMock Isolator. But what about writing unit tests for newly written code? Is it a good practice to use an ...
1 vote
3 answers
153 views

Creating Unit and Integration Tests with Database elements

This is something that I've heard a number of opinions and theories about, but I'm still torn on how to go forward. For context, this particular issue deals with the following technologies, in case ...
40 votes
12 answers
26k views

How do we make unit tests run fast?

We have reached the point in our project where we have almost a thousand tests and people have stopped bothering with running them before doing a check in because it takes so long. At best they run ...
2 votes
2 answers
344 views

Should mocks used in unit tests represent entireties or subsets?

I was working on a unit test that has a mock object looking something like: public class TestMock { static { Doodad a = new Doodad(0); Doodad b = new Doodad(1); Doodad c = ...
3 votes
3 answers
1k views

TDD when removing long lived feature

Suppose I had some Manager class that I need to change in regards to existing functionality by removing code. The Manager always sends an initial message after a connection was established to do an ...
1 vote
1 answer
102 views

Unit Test Against In-Memory vs Real Database for SwiftData Applications

I am writing unit tests for my SwiftData application. Currently, I am using in-memory database, which get reset after every test. What benefits will I gain if I start using real database? My main ...
3 votes
2 answers
920 views

Integration Testing Stripe Payments - stripe-mock or Stripe's test mode?

I'm building an automated test suite to integration/feature test my organization's Stripe implementation within a GitLab CI pipeline. We're running Laravel 8 and using phpunit for testing. Within this ...
1 vote
1 answer
96 views

Benefits of resolving dependencies with IServiceCollection and IServiceProvider for Unit Tests instead of inheriting mocks from a base test class

Articles such as this point out some of the pitfalls of manually instantiating dependencies in UTs, while showing some of the benefits of doing it, instead, with the .NET's dependency container by use ...
-3 votes
1 answer
87 views

Testablilty of setup with builder pattern for configuration

We have built a library which can handle RESTful requests based on configured endpoints. A fluent builder is being used to create endpoint definitions (configurations). These definitions are bound to ...
2 votes
2 answers
185 views

How do we call the approach to unit test through an additional layer of abstraction? [closed]

There‘s an approach in unit testing where, instead of just writing the technical details in every test method, you create a library of helper functions like givenServiceFails (that acts on mock ...
4 votes
5 answers
364 views

Does testing the public surface of a class test the behavior of code or the way it is written?

A follow up question to How do unit tests facilitate refactoring without introducing regressions?. I said that integration tests test the behavior of the code, while unit tests, being tied to ...
38 votes
12 answers
7k views

How do unit tests facilitate refactoring without introducing regressions?

We all know the standard TDD cycle: I first write a unit test, the smallest unit test that fails; then I write some production code, again the smallest amount of production code that is sufficient to ...
0 votes
1 answer
90 views

Testing Boundaries on Publisher/Subscriber patterns

I work with a integration project based on Publisher/Subscriber pattern whose subscriber end feeds a staging database which stores data to be pushed to another application. The staging database has ...
0 votes
3 answers
346 views

Would you test this piece of configuration code? How do I determine which code is worth testing?

We have a piece of code that decorates an interface to transparently add retry logic. Inversion of Control configuration service.AddOurRestApiClient() .AddResilienceHandler("Retry", ...
0 votes
2 answers
84 views

EFCore In Memory repository Unit Test of Encrypted data

I have in project linq repository unit tests. [Fact] public async Task Get_FromTreeEntitiesUnsorted_RetunsOrderByDescending() { //Arrange _contextReadonly.Entity.Add(new ...
1 vote
4 answers
1k views

Should the unit tests be independent of the implementations

In the book- The Art of Unit Testing, I have read that the changes to the code base should not change the unit tests. But I have a hard time figuring out what should not be changed in the unit tests. ...
57 votes
7 answers
14k views

Should unit tests be stored in the repository?

I'm a growing programmer who's finally putting unit testing into practice for a library that I'm storing on GitHub. It occurred to me that I might include the test suites in the repo, but as I look ...
2 votes
2 answers
335 views

Unit testing for non-stateless units - how?

Firstly let me say I have never written a unit test in my life. I am trying to get the hang of PHPUnit, and so far it's working pretty well for me so far as "stateless" functions (that ...
9 votes
3 answers
6k views

How should source code unit tests affect semantic versioning?

I recently started using Semantic Versioning. Due to conflicts with my previous versioning convention (which I created myself), I am very confused about versioning unit tests as part of the public API....
1 vote
1 answer
328 views

Is there a best practice for storing certificates (e.g. x509), which include private keys, used in unit tests?

In a software library, I wrote, large parts of the code use x509 certificates for various purposes like signing documents digitally. Is there a best practice for storing test certificates used for the ...
2 votes
1 answer
85 views

What is the correct way to configure a testing mode on a class?

I have a repository which reads and writes to Firestore, and some tests to make sure data is sent and comes back in the correct way. In order to test this I added a protected function which returns ...
121 votes
19 answers
15k views

What is the point of unit tests?

I've been a software developer for 20+ years, although it's been an unusual career. I've mostly worked on either legacy projects, or small standalone, non-public-facing apps and so only a couple of ...
539 votes
18 answers
483k 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 tests ...
1 vote
3 answers
419 views

Is low code quality and lack of testing the norm in B2B software development?

I've been working at this company for about a year now. it's a growth company working in B2B. I'm one year out of university, with a major in computer science. I work in the web team, using an Angular ...
5 votes
1 answer
21k views

Using Mock for event listeners in unit-testing

I keep getting to test this kind of code (language irrelevant): public class Foo() { public Foo(IDependency1 dep1) { this.dep1 = dep1; } public void setUpListeners() { this.dep1....
58 votes
12 answers
6k views

How to test the tests?

We test our code to make it more correct (actually, less likely to be incorrect). However, the tests are also code -- they can also contain errors. And if your tests are buggy, they hardly make your ...
6 votes
4 answers
4k views

Does TDD formally use black box testing to supplement unit tests?

Unit tests are never perfect at capturing functionality, particularly in certain parts of an application (such as the GUI), so everyone needs some measure of black box testing. Does TDD have anything ...
9 votes
5 answers
16k views

How to write my own unit test?

I am currently trying to write a unit test of my own. The unit test I am writing is mainly for linear algebra (I know there are great package out there but I want to use my own for my project). My ...
0 votes
1 answer
889 views

Why are sealed classes not allowed to be mocked?

Using Moq framework, it is not allowed to mock a class in C# which is sealed. Same goes for many other frameworks as well. But why is it not allowed?
0 votes
0 answers
58 views

Is using @Transactional together with @Test annotation on the same test method a good practice?

In my current Java project, there is such style of integration test: @Test @Transactional public void testBusinessTransaction() throws Exception { //...complicate set up code and execution code ...
1 vote
2 answers
195 views

Is it possible to prove mathematically that unit testing reduces test effort as compared to integration testing? [closed]

I took out the request for a paper, thank you for spending so much time on this. hopefully this edit is better. I use the word "complexity" meaning "cyclomatic complexity" so if ...
3 votes
1 answer
3k views

How do sites like LeetCode & HackerRank test your code for correctness?

Not sure if this question belongs here, but I've always wondered how exactly sites like Leetcode & HackerRank test your code. Say you code in Java for a question like, Find all the duplicates in ...
53 votes
10 answers
7k views

How should I test "Glue Functions" without testing that "the code I wrote is the code I wrote"?

I usually write my code in a test driven style. I write tests as specifications and then my code. It's great and useful. I always try to ignore implementation when testing and only test behaviour. I ...
1 vote
2 answers
74 views

Test-doubling a file path in Java/Groovy

I am trying to write a unit test for a helper method in my ApiRequestHandler which handles APIGatewayProxyRequestEvent . This class uses an EFS file path, which is where it writes files to. I have ...
16 votes
6 answers
9k views

Should I Have One Interface Per Class For Unit Testing?

Should I define an interface for every public behavior class (excluding data classes)? I've spent many hours searching and reading to find a clear answer. If I search "Do you define an interface ...
2 votes
3 answers
261 views

How do you decompose big object for testing?

I have a package that provides an object with quite a lot of features owned by it. Let us say the object is an HTTPServer, and when the user initializes it by providing config and a request handler ...
-1 votes
1 answer
233 views

Alternatives to using mock libraries such as Mockito in unit testing

Typically, when writing unit tests I tend to need to stub out collaborators and also mock some behavior in one or more of the collaborating objects. Say if I am testing a Service that is using a Dao, ...

1
2 3 4 5
36