How to test & code coverage
Posted by milofishiee@reddit | learnprogramming | View on Reddit | 7 comments
I just started working after university and realized I know next to nothing about testing. I did some searches and got bits and pieces, but I can't wrap my head about how to do it. Which types of testing do a programmer need to do, as the project progresses? Do I need to write unit test for every methods/functions I wrote?
More specifically, I'm working on a spring backend project with multiple microservices, can/how should I write tests for usecases?
TopClassroom387@reddit
I work as a professional programmer with procedural languages, mainly C# these days.
The different types of testing you will encounter are
* Unit Testing - done by the developers
* Integration Testing - done by the developers
* System Testing - done by Test Team (developers can double-hat as Testers for this part)
* User Acceptance Testing - done by users or their representatives (Business Analysts, Test Team and not developers)
For Unit Testing, I've always taken the viewpoint of:
a. Exercise the code - given a variety of inputs, what will happen and what will the output be?
b. Prevent regression - ensure future updates to the code do not break what currently works.
c. Demonstrate functionality - If you have a complex process that requires data to be setup in a particular way, this will demonstrate how to make it happen.
More specifically, on How to Test. You will read and see lots of different ideas on this. Pick one that you like the look of, feel comfortable working with and can use without too much trouble. There are lots of advanced cases and setups. To name a few things...Dependency Injection, Mocking, Subtitution. What works and how you achieve it will depend on the language and the environment you are in. And, it also depends on the technology being used.
Do you need to test every function you write?
This is something you should aim to do as it means you are certain of what your code does and you don't have redundant code in place. It will also help you write better programs as you will be thinking "How can I test this?" as you write.
Don't be shy in asking a question in the right forum, or better, searching the internet for examples.
As you say you are working just out of University, I expect your employer will be providing somone to guide you in what they consider Best or Good Practive.
Enjoy programming, it can be fun.
milofishiee@reddit (OP)
Thanks for the detailed response! If I understand correctly, integration testing can/should be automated, while with system testing it's not always the case?
bestjakeisbest@reddit
Unit tests to test each part in a vacuum, basically say you made an implementation of an array list, you would want to test that you can store things, and retrieve things from the array.
Then you want to run integration tests, these put your unit tested code into a real world use case and makes sure it behaves.
There are testing libraries out there for basically every language but worst case they can be implemented as a bunch of smaller programs (although this isn't typically something you would ever want to really do since its a lot of boiler plate code for each test)
optical002@reddit
For me, I do unit tests to get a guarantee that this function/method works as expected.
Some functions are self explanatory and it is redundant to write a test for it, but when I look at the function and see that it is hard to predict what the output will be given its input, then I write a test which gives me a guarantee that it will not malfunction, or give unpredicted results.
With strongly typed languages unit tests become very redundant since compiler makes sure it works as expected. With dynamic languages compiler isn’t that friendly to you and gives runtime errors instead of not compiling, this is where unit testing helps a lot to give guarantees that the code will work and not crash.
Fred776@reddit
I don't think this is true. All the compiler can do is to ensure all your types make sense. It can't tell whether you have implemented your algorithm correctly.
optical002@reddit
I agree, if a function has an algorithm then testing is still needed.
As i come from functional programming world, I write code which solves problems by expressing it via ADT (Algebraic Data Types) and pure functions which manipulate data.
For example I have data types which are actually instructions, then with functions I have instructions which needs to run, and then I write an interpreter, which interprets data and runs the program. Similarly how python interpreter works.
In those cases compiler helps a lot, since mostly functions in that kind of program are pure data transformations and if something does not add up or is missing compiler will tell you.
Sometimes its more efficient to wrap some complicated behaviour in a single function and algorithm, where testing is needed
Glittering-Guess2560@reddit
testing techniques