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.

6
votes
2answers
190 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 ...
89
votes
17answers
26k views

Why are unit tests failing seen as bad?

In some organisations, apparently, part of the software release process is to use unit testing, but at any point in time all unit tests must pass. Eg there might be some screen which shows all unit ...
7
votes
5answers
652 views

With TDD, tests obviously fail first.But is that really true?

I have been taught that with TDD, the tests "naturally fail first but it is a good habit to run them anyway to see the red light". Well, but I am quite sure a unit test written first for a new feature ...
1
vote
1answer
348 views

How to pass a mock object into a class for unit tests

There seem to be many ways to pass a mocked object into a class for unit testing and I am unsure which is the proper approach to take for my PHP application. If I was using Dependency Injection then ...
3
votes
4answers
134 views

What is the best practice for a regression test that wants to know about encapsulated information?

Suppose you have two classes: Alpha, and Bravo. Alpha constructs a new Bravo class during its own constructor and exposes no public visibility to it. Bravo has a String named charlie (and a method to ...
0
votes
1answer
55 views

Refactoring code which uses a global list and updates list based on messages from an external system

I am refactoring some code where a globally defined list is updated based on messages from an external system. Currently there is a global instance of the list. Various messages update the list ...
1
vote
4answers
373 views

Are parsers a special case in unit testing?

Context While trying to get my parser classes under test, I noticed a common challenge for (unit-)testing them: They have only one public method, with a string as input and the parsed class as output....
-1
votes
1answer
39 views

Testing Application Helper that Depends on Controller

I am working on practicing TDD/BDD/ATDD and I am working on a rails application. I created a helper method that depends conditionally on the type of controller being tested. module ApplicationHelper ...
1
vote
0answers
59 views

Unit testing EventArgs - will there be timing issues?

I am writing a small game library. I capsule the logic in my model named Block. My model Block communicates only via events to it's clients (Is this a good thing to do!?). I wrote a simple unit test ...
1
vote
1answer
34 views

Worth it to test client side element presence for web application?

I've really been focusing on making my tests maintainable by focusing on BDD-style tests and only focusing on interfaces. I do not want my tests to be brittle and prevent refactoring. Is it worth it ...
1
vote
1answer
95 views

Fixtures, Tests, and Preconditions

When using factories, although more work is required up front, you know that you will be setting the correct pre-conditions for your test (up to what you have written.) However, when you use fixtures, ...
1
vote
2answers
146 views

When unit testing is not enough (what error cannot be found besides GUI)? [closed]

I am thinking, if I do proper testing for all methods in all my classes, then - disrgegarding UI - do I still need system testing? I mean, what would a simple example be, showing a case where unit ...
21
votes
5answers
4k views

Testing a list…All in the same test or one test for each condition?

I'm testing that a function does what expected on a list. So I want to test f(null) -> null f(empty) -> empty f(list with one element) -> list with one element f(list with 2+ elements) -> ...
1
vote
2answers
380 views

TDD workflow with integration testing

I'm having troubles with using the right workflow with TDD. Some people say we should design before writing any code, some say we should make a test, make it pass, then refactor the code and that ...
5
votes
3answers
1k views

What is the correct approach to test classes with inheritance?

Assuming I have the following (over-simplified) class structure: class Base { public: Base(int valueForFoo) : foo(valueForFoo) { }; virtual ~Base() = 0; int doThings() { return foo; }; ...
0
votes
3answers
156 views

How to design a unit test to safeguard against internal state dependency

I'm doing TDD. Let's say I expect some method m of some class C to call some function f (to keep things really simple). How would I design a unit test which would pick-up and therefore fail, if m ...
-1
votes
5answers
471 views

Is it good practice to have a unit test for a simple data method?

I have a simple data method that does this: public void Write(Foo foo) { db.Foos.Add(foo); db.SaveChanges(); } I was asked to write unit tests for this. To do so, I had to create a fake ...
81
votes
6answers
16k views

Is it bad practice to enforce an execution order for unit tests?

I am writing tests for a project that consists of multiple submodules. Each test case that I have written runs independent of each other and I clear all data between tests. Even though the tests run ...
5
votes
1answer
644 views

How to test interactors in clean architecture?

After reading the last book from Robert C. Martin, I've tried a to develop some big Go applications following clean architecture. While writing interactors, I end up with a lot of complex unit tests, ...
3
votes
1answer
131 views

Should BDD scenario be as minimal as possible, or is doing multiple cases in them okay?

Suppose were testing line segment intersections, and the intersections consider parallel and collinear lines to never intersect. Which one would be more natural to write? One case has more jammed ...
95
votes
11answers
16k views

How do you detect dependency problems with unit tests when you use mock objects?

You have a class X and you write some unit tests that verify behaviour X1. There's also class A which takes X as a dependency. When you write unit tests for A, you mock X. In other words, while unit ...
0
votes
2answers
181 views

How should I structure an automated test with a dependency on an HTTP endpoint?

I'm trying to better understand unit tests and integration tests. I understand unit tests should be in isolation and their dependencies should be mocked. But I'm slightly unclear on the best approach ...
37
votes
7answers
7k views

Is constructing objects with null parameters in unit tests OK?

I started writing some unit tests for my current project. I don't really have experience with it though. I first want to completely "get it", so I am currently using neither my IoC framework nor a ...
-5
votes
1answer
146 views

What is faster, to read 100MB from file or to compile 100MB of code?

I want to generate a test scene for a unit-test (big file parser). What will be faster, to have a test file and to perform I/O on it, or to generate a large memory buffer using some static parameters ...
5
votes
5answers
926 views

How to Unit-Test a parser of a file?

I'm implementing a metadata parser of image files from all formats. I want to write tests for it. One trivial way to do so is to have test image files of all formats as a resources for the tests, and ...
6
votes
6answers
1k views

Why wouldn't my team want to run unit tests automatically?

I'm managing a team in .net. They are writing unit tests, use them locally on a regular basis, and love it. However, they keep pushing to have the unit tests kept as a separate project and only want ...
1
vote
1answer
431 views

How to unit test database queries based on current date?

Is there a feasible way of testing SQL queries that contain things like CURRENT_DATE and NOW() in a unit test? For example: public function deletePastEntries(): bool { $sql = "DELETE FROM queue ...
2
votes
1answer
82 views

Keep unit tests common in separate library or duplicate?

Sometimes, ex. when using common mocks (same for data access layer as for presentation, I use often xunit ClassData), I come to the moment when I either copy common code to new unit test library or ...
7
votes
4answers
344 views

A few classes versus 20 classes? A question about bloat [closed]

In evaluating the work of an outsourcing company, I have found that they have a propensity for dividing work into as many classes as possible, each one being testable. In some ways this seems ...
3
votes
3answers
423 views

Should //given //when and //then comments be included in unit tests?

I've joined a new team of great colleagues and a difference of opinion has come up concerning including //given //when and //then comment blocks in unit tests. For example public void ...
3
votes
1answer
366 views

Does using fixtures make a unit test an integration test?

Does using touching a database mean a unit test is an integration test even if you are using fixtures? If so, what are the alternatives to write a unit test when you need to use models as a part of a ...
3
votes
2answers
221 views

Test Internals with TDD?

I'm new to TDD, and relatively new to software development in general (e.g. < 4 years experience), but I am trying to learn. I have been toying with TDD but ran into what I know realize is a ...
0
votes
0answers
75 views

Requiring developers to unit test their code [duplicate]

I work at a small company, and I am tasked with designing a good policy regarding TDD and unit testing that encourages developers to write unit tests. As of now, I am the only developer who ...
3
votes
2answers
118 views

Is it acceptable to test based on test output data rather than input data in unit tests?

I'm used to write unit tests with assertions which are based on the input, e.g. (hopefully self-explanatory and let's assume that using random test data is fine) int a = random(); int b = random(); ...
2
votes
5answers
319 views

Unit testing and other forms of testing

This may be a silly question but if I have good unit test coverage, does that mean I can reduce the amount of, or remove completely, functional and integration testing. When people talk about having a ...
2
votes
1answer
174 views

Is it a best practice to use a mocking service for testing http requests?

Is it better to unit test using a mock library like 'nock' (nodejs) or to just test the server's http requests directly? Here is an example of my Express server test for testing if my server is up ...
0
votes
1answer
86 views

What are the dangers/downside of unit tested dynamic library(DLL) being different from released library?

This is similar to: https://stackoverflow.com/questions/1278831/unit-tests-in-production-release-code# but more specific. At the moment the DLL I ship and the DLL I use for unit testing have ...
1
vote
2answers
145 views

Should I write integration test that test my code with real-network servers, before I start writing my code?

I am trying to write integration tests for a client-server app. The client app runs on a user's machine and connects to the server app running on a specific Windows server, network-serverA. The ...
2
votes
1answer
144 views

Making a test fixture inherit from the SUT class

Some time ago I had to code review a unit-test fixture like this one (C# code): public class ServiceFixture : Service { [Fact] public void Test_DoSomething() { Assert(DoSomething()...
0
votes
1answer
159 views

Design Pattern for doing things in sequence and being testable

I'm working on an application which contains an element that needs to make several http calls in a set order, to perform an action successfully. (Interfacing with an external system's API.) We have ...
0
votes
2answers
109 views

How to identify test result as “ignore” (not successful or unsuccessful) in process of execution?

We use Selenium webdriver + SpecFlow for testing our Web application. For some functionality, everything is clear. But imagine the situation: some questions arrive to Web user from external system he ...
1
vote
2answers
253 views

Does it matter how I setup test data when creating unit tests?

I have a unit test similar to the code snippet below, it should check that the AddUser method only allows unique emails. My question is around the Arrange part of this unit test, I use existing ...
1
vote
1answer
2k views

Writing Junit tests for catching exception?

I have a function/method which throws some exception when database is being called. I was writing Junit tests and I was told to have good coverage. So, should I write Junit test which catches ...
2
votes
3answers
134 views

Using methods that are not under test within a unit test for a different method?

I'm building a Sudoku generator. I have a board class with a number of methods: public class Board { public Board() { /* Creates an empty board */ } public bool ValidateRow(int row) { /* ...
1
vote
2answers
189 views

Making public methods virtual to ease testability

I have been looking around for some practices to write testable code and gathered the following: • Public methods virtual if not using interfaces -- makes mocking easier • Dependency injection -- ...
37
votes
7answers
10k views

Is using “new” in the constructor always bad?

I have read that using "new" in a constructor (for any other objects than simple value ones) is bad practice as it makes unit testing impossible (as then those collaborators needs to be created too ...
-2
votes
1answer
96 views

Android testing, what to test? [closed]

I'm quite a noob in the world of testing. I've read all the materials related to Android testing: Unit test Instrumentation test JUnit Robolectric Espresso etc. The thing is that, all the examples ...
5
votes
1answer
341 views

Design Patterns to model complex decision graph

I am currently building a piece of code that creates side-effects based on input parameters. It has around ten input parameters and about six available side-effects. Based on the input parameters, the ...
1
vote
4answers
367 views

Unit test a generic floating point equality function

I've written a function that tests two floating point numbers for approximate equality (see this Code Review question). I'd like to unit test my function, but I'm not positive of the best way to do ...
11
votes
2answers
3k views

Unit Test to test the creation of a Domain Object

I have a Unit Test, which looks like this: [Test] public void Should_create_person() { Assert.DoesNotThrow(() => new Person(Guid.NewGuid(), new DateTime(1972, 01, 01)); } I am asserting that ...