Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should all unit tests be successful?

Let's say you make an application that attempts to transliterate stuff from alphabet A into alphabet B, as closely as possible.

Because language B is very complex, this is not always successful. But you do get an approximate transliteration.

How would you build unit tests in this case, considering that you expect 20-30% to fail?

like image 324
ellabeauty Avatar asked Aug 12 '12 18:08

ellabeauty


People also ask

Should all unit tests pass?

At each check-in, all tests should pass. You'll see tests fail initially when you do TDD, and that's the reason you can trust them. As part of the TDD red/green/refactor cycle, though, at the end of each iteration, you should be left with an all green test run.

Are unit tests always necessary?

One of the benefits of unit tests is that they isolate a function, class or method and only test that piece of code. Higher quality individual components create overall system resiliency. Thus, the result is reliable code. Unit tests also change the nature of the debugging process.

How do you know if a unit test is good or bad?

A good unit test encompasses many different attributes or practices, including that it should: Be small and isolated. Be about something highly specific regarding the code. Have a ratio of testing to assertion near 1, thereby making it easier to identify any failed assertion.

What makes good unit tests?

Unit testing guidelines demand that a good unit test should be independent, repeatable, readable, exhaustive, realistic, maintainable, fast and easy. These are the best practices you want to keep in mind in order to write effective unit tests and make sure the time invested is truly useful.


3 Answers

It must always be the goal that your unit test is successful. The way you use it, you can not differentiate between serious errors in your software and the errors in the translations you seem to expect.

I would suggest to separate both:

  1. Use unit tests only to make sure that your software works the way you expect it (even if this includes some mistakes in the translations)
  2. Produce a test dataset containing the translations produced by your software. You can check this test dataset either manually or using a statistical software package like R to get an idea of the quality of your translations.
like image 181
Marcel Hebing Avatar answered Oct 28 '22 06:10

Marcel Hebing


Unit test should be deterministic. Failing test should indicate software failure, not something that "works as intended". For your case, prepare data in a way you can be sure of results and test for them, whether conversion succeeds or fails (testing for failure is always an option, given you expect failure - important is that you're always in control of when your test passes/fails).

like image 42
k.m Avatar answered Oct 28 '22 06:10

k.m


A unit test that is expected to fail is not a unit test. You need to change the definition of success by using an evaluation function that acts as a filter and decides if it is "close enough" and determines pass/fail. As your translator gets better, you can raise the bar in the filter.

like image 38
amdn Avatar answered Oct 28 '22 06:10

amdn