What are some real world examples of great unit tests? Links to Github repos welcome

I think the title says it all. I’m learning how to write better unit tests and I think that a great way to do it would be to look at some real, production sofware that has really good unit test written. Can you recommend some examples? The language doesn’t matter.

How to Automate Unit test for HMI application?

I have the requirement like this: . Write python script, which run on the test Machine and interact with HMI Application.

· Automatic test can trigger the command from HMI (Button click action) / take the screen short of widget / screen for the status indication and compare it with expected results (compare with expected value/ pixmap) .

· Automatic test run every night and generate the test report of executed test cases.

Please, suggest some good solutions as this is very urgent.

Thanks, Aniprada

Does it make sense to write one unit test and loop it through similar components?

I have a situation where I have different forms, each with 4 or 5 steps (components), and I decided to write one unit test per step.

After doing it, I noticed they were very similar and I could just loop through them, changing some values based on the loop index. All good and worked fine, until one of the forms failed and I couldn’t figure out which step was failing . I could provide another generic solution for it, but this failure made me wonder if it really makes sense to reuse code for tests like this.

Well… as developers, we always want to reduce code duplicates, but I think for unit/snapshot tests, it’s a best practice to have every test explicit. The advantage I see by duplicating code in this situation is that it makes it more clear, easy to debug when a failure happens and also, once it’s test code, it doesn’t impact production code. What are your thoughts on it? Does it worth to have generic code to test several similar components, or is it better and safer to have it more explicit, even if you have to duplicate the code?

I also read this article on twitter, a few days ago that opened my mind for this approach even more: https://www.sandimetz.com/blog/2016/1/20/the-wrong-abstraction WDYT?

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:

  1. They should not break by any unrelated code change elsewhere in the codebase.
  2. Only one unit test should break by a bug in the tested unit, as opposed to integration tests (which may break in heaps).

All of this implies, that every outside dependency of a tested unit, should be mocked out. And I mean all the outside dependencies, not only the “outside layers” such as networking, filesystem, database, etc..

This leads to a logical conclusion, that virtually every unit test needs to mock. On the other hand, a quick Google search about mocking reveals tons of articles that claim that “mocking is a code smell” and should mostly (though not completely) be avoided.

Now, to the question(s).

  1. How should unit tests be written properly?
  2. Where exactly does the line between them and integration tests lie?

Update 1

Please consider the following pseudo code:

class Person {     constructor(calculator) {}      calculate(a, b) {         const sum = this.calculator.add(a, b);          // do some other stuff with the `sum`     } } 

Can a test that tests the Person.calculate method without mocking the Calculator dependency (given, that the Calculator is a lightweight class that does not access “the outside world”) be considered a unit test?

Struggling with cyclical dependancies in unit tests

I’m trying to practice TDD, by using it to develop a simple like Bit Vector. I happen to be using Swift, but this is a language-agnostic question.

My BitVector is a struct that stores a single UInt64, and presents an API over it that lets you treat it like a collection. The details don’t matter much, but it’s pretty simple. The high 57 bits are storage bits, and the lower 6 bits are “count” bits, which tells you how many of the storage bits actually store a contained value.

So far, I have a handful of very simple capabilities:

  1. An initializer that constructs empty bit vectors
  2. A count property of type Int
  3. An isEmpty property of type Bool
  4. An equality operator (==). NB: this is a value-equality operator akin to Object.equals() in Java, not a reference equality operator like == in Java.

I’m running into a bunch of cyclical dependancies:

  1. The unit test that tests my initializer need to verify that the newly constructed BitVector. It can do so in one of 3 ways:

    1. Check bv.count == 0
    2. Check bv.isEmpty == false
    3. Check that bv == knownEmptyBitVector

    Method 1 relies on count, method 2 relies on isEmpty (which itself relies on count, so there’s no point using it), method 3 relies on ==. In any case, I can’t test my initializer in isolation.

  2. The test for count needs to operate on something, which inevitably tests my initializer(s)

  3. The implementation of isEmpty relies on count

  4. The implementation of == relies on count.

I was able to partly solve this problem by introducing a private API that constructs a BitVector from an existing bit pattern (as a UInt64). This allowed me to initialize values without testing any other initializers, so that I could “boot strap” my way up.

For my unit tests to truly be unit tests, I find myself doing a bunch of hacks, which complicate my prod and test code substantially.

How exactly do you get around these sorts of issues?

Unit testing as non developer

How possible is it for a non-dev, Qa person, to on board themselves to learn how to unit test production code?

SI have an understanding of how code is written and understand basic logic but what do you consider the learning curve would be for me if I wanted to learn how to write unit tests for my team? Not overnight obviously, but over time? Is it even feasible?

Is there a unit of measurement that can express code execution speed in absolute terms?

I’ve always seen code execution speed measured either in units of time (e.g. t milliseconds), or using asymptotic analysis (e.g. O(n log n)). These methods are relative to hardware performance though, they’re not absolute terms.

For space performance, we have the same asymptotic analysis, but we can also measure that performance in bytes, which allows us to express (and predict) space performance in both relative and absolute terms. e.g. algorithm X’s space complexity is O(n) or n * 32 bytes of memory for implementation Y in language Z.

For example we can look at this code:

for i in n:     pass 

And if we know this will be executed using a 64-bit build of CPython, we can say this for loop will take up 72 + n * 8 bytes for the integer array and 8 bytes for each reference (independent of context/overhead).

My question is: Is there a unit of measurement we can use to express a piece of code’s execution speed (or CPU usage) in absolute terms, similar to bytes for memory?

How to write DRY Unit Tests when order of method calls matters

Suppose I’m writing a calculator engine with a class as follows…

public class ArithmeticExpression {     ArithmeticExpression Add(double operand) { /* ... */ }     ArithmeticExpression Subtract(double operand) { /* ... */ }     // ... other useful operations like division, multiplication, etc.     double ProduceResult() { /* ... */ } }  

… and the goal is to defer evaluation of the expression until ProduceResullt() is called, so that correct order of all stacked operations is maintained.

My question is:

How should I tackle with unit testing this class without getting overwhelmed by the number of combinations of method calls?

It is clear to me that the order of calls should be tested somehow, but writing tests like this:

public void Multiply_CalledAfterAdd_TakesPrecedenceOverAdd() {      // ... } public void Multiply_CalledAfterDivide_EvaluatesLeftToRight() {     // ... } 

can get maddening and out of control quite quickly. And it’s not even about those 16 tests for the four basic arithmetic functions. Suppose in the future I decide to extend the calculator with a modulo operation – that’s 9 almost identical tests more to begin with.

On the other hand – the whole puprose of the class is to keep the order of operations maintained, so it’s got to be verified somehow.

Wrapping up – I know the example is trivial, but it’s a general problem that I struggle to find an answer to. Any help is appreciated, thanks.