What should be tested when writing unit tests?

I want to start unit testing our application, because I believe that this is the first step to developing a good relationship with testing and will allow me to switch to other forms of testing, the most interesting BDDs with Cucumber.

We are currently generating all of our base classes using Codesmith, which are entirely based on tables in the database. I'm interested in learning about the benefits of creating test cases with these base classes? Are these bad testing methods?

This leads me to the final question about my post. What do we test when using unit tests?

Are we checking the examples that we know we want? or are we checking examples we don’t need?

There may be methods that have several solutions and multiple methods of success, how do we know when to stop?

Take the sum function, for example. Give it 1.2 and expect 3 in a single unit test .. how do we know that 5.6 won't return 35?

Question

  • Generating unit tests (good / bad)
  • What / how much are we testing?
+7
unit-testing testing code-generation
source share
7 answers

The point of unit tests is to give you confidence (but only in special cases it gives you certainty) that the actual behavior of your public methods is consistent with the expected behavior. So, if you have an Adder class

 class Adder { public int Add(int x, int y) { return x + y; } } 

and corresponding unit test

 [Test] public void Add_returns_that_one_plus_two_is_three() { Adder a = new Adder(); int result = a.Add(1, 2); Assert.AreEqual(3, result); } 

then this gives you some (but not 100%) confidence that the test method is behaving properly. It also gives you some protection against code breaking during refactoring.

What do we test when using unit tests?

The actual behavior of your public methods versus the expected (or specified) behavior.

Are we checking the examples that we know we want?

Yes, one way to gain confidence in the correctness of your method is to take some input with a known expected output, execute an open method on the input, and compare acutal output with the expected output.

+6
source share

Start with your requirements and write tests that test expected behavior. From now on, how many other scenarios you are testing may be due to your schedule or perhaps your signs of failed scenarios that are especially at risk.

You can think of writing tests without success only in response to the defects you discovered (or your users) (the idea is that you write a test that checks for a defect before actually fixing the defect so that your test fails if this defect will be reintroduced into your code in a future development).

+7
source share

What to check: everything that ever happened.

When you find an error, write a test for behavior with an error before , you will correct the code. Then, when the code works correctly, the test will pass, and you will have another test in your arsenal.

+4
source share

1) To start, I would recommend that you test the core logic of the application.

2) Then use the code coverage tool in vs to see if all your code is used in tests (all if-else branches, case conditions are called). This is some answer to your question about testing 1 + 2 = 3, 5 + 6 = 35: when the code is covered, you can feel safe with further experiments.

3). Good practice is to cover 80-90% of the code: the rest of the work is usually inefficient: getters-seters, handling exceptions from 1 line, etc.

4) Learn about sharing problems.

5) Testing the generation module - try it, you will see that you can save beautiful lines of code by writing them manually. I prefer to generate the file using vs and then write the rest of TestMethods myself.

+3
source share

You arrange things where you

  • want to make sure your algorithm works.
  • want to protect against accidental changes in the future

So, in your example, it would not make sense to test the generated classes. Instead, test the generator.

It is good practice to verify the main use cases (which was designed for the first function). Then you check for basic errors. Then you write tests for corner cases (i.e. lower and upper bounds). Unusual cases of errors are usually so complex that it makes no sense to test them.

If you need to test a large set of parameter sets, use data-driven testing.

How many things you test is a matter of effort or return, so it really depends on the individual project. Usually you try to follow the 80/20 rule, but there may be applications where you need more test coverage, because a failure will have very serious consequences.

You can significantly reduce the time it takes to write tests if you use the test approach (TDD). This is because code that is not written with the ability to test is much more complicated, sometimes almost impossible to verify. But since nothing in life is free, code developed with TDD tends to be more complex.

+2
source share

I also begin the process of using unit tests more consistently, and I find that the biggest challenge in unit testing is to structure my code to support testing. When I start thinking about how to write tests, it becomes clear where the classes have become overly related, to the extent that the complexity of the β€œunit” makes it difficult to define tests. I spend so much or more time refactoring my code as I write tests. Once the boundaries between the units being tested become clearer, the question of where to start testing resolves itself; start with your smallest isolated dependencies (or at least those you worry about) and walk your way up.

+1
source share

I am testing three main events: min, max and somewhere between min and max.

And if necessary, two extremes: below min and above maximum

There are obvious exceptions (for example, some code may not have min or max), but I found that unit testing for these events is a good start and fixes most of the β€œcommon” problems with the code.

+1
source share

All Articles