Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I unit test my /src files or /build files?

When setting up unit tests (in my case, using Jasmine for JavaScript) should the un-minified/in-uglified src files be tested? Or should the end-user build files (minified and uglified) be tested?

In my grunt config:

jasmine: {
    src: ['src/file.js']
}

vs.

jasmine: {
    src: ['build/file.min.js']
}

On one hand, it's nice to test the src files since it doesn't remove debuggers and is easier to inspect when needed.

On the other hand, I can test the src files as much as I like but it's not true to what the end users will be running, as the build files are uglified and minified.

like image 258
d-_-b Avatar asked Jul 15 '15 15:07

d-_-b


People also ask

Should unit tests write files?

There is a general rule to be cautious of writing unit tests that do file I/O, because they tend to be too slow. But there is no absolute prohibition on file I/O in unit tests. In your unit tests have a temporary directory set up and torn down, and create test files and directories within that temporary directory.

Should repositories be unit tested?

Yes, you should test your repository layer. Although the majority of these tests fall into a different classification of tests. I usually refer to them as integration tests to distinguish them from my unit tests.

Where should the unit test reside in the repository?

The developers should write unit tests alongside the source code and execute them in pipelines. Separating the repositories keeps more clean environment.

What is the best way to unit test a method that doesn't return anything void )?

The most popular option to verify the output when testing the void method is to use the mock object. A mock object is an object that replicates the behavior of a real object for the purposes of unit testing. For example, you can observe how the class under test calls the dependency with a mock object.


1 Answers

You should definitely unit test the bare source files as there were written. Unit tests are designed to be tightly focused and give you instant and clear feedback. In other words, if you test your functionality after the source code being modified - you are not testing your code in isolation; once you discover a bug, you cannot be 100% sure what caused it - it could be that there was a problem during the build that caused the problem - say the uglify task.

I would classify testing build files as a part of integration or a higher-level testing - since aside from testing functionality, you are also checking how your application was built: how the files were minified, copied, concatenated etc.

In general, you should aim to the following pyramid:

enter image description here

(introduced in this Google Test Automation blogpost)

On the other hand, I can test the src files as much as I like but it's not true to what the end users will be running, as the build files are uglified and minified.

I think this goes under the End-to-end testing category - imitating a real user, going through acceptance-testing scenarios, user stories.

like image 154
alecxe Avatar answered Sep 21 '22 12:09

alecxe