It may seem silly, but unit tests are useful for the real?

Possible duplicate:
Is unit testing worth the effort?

Well, I know what the purpose of unit tests in general is, but I found some things that bothered me.

1) The purpose of testing for early detection of errors. So, in some later iterations, if I make some changes to the code, automatic testing should bother me and tell me that I messed up a long-forgotten piece of my software.

But say that I have class A and say that it interacts with an instance of another class, call it class B.

When you write unit test for class A, it should mock class B. So, in some future, if you make some changes to class B, and this causes some errors, they will only reflect in class B unit test, and not in (because the test does not use real class B, but it is a layout with fixed inputs and outputs). So, I don’t see how unit test to make early notification of errors that no one knows about? I know about the possible errors in the class that I am changing, I don’t need a unit test for this, I need a test to warn me about the consequences of my changes, which makes an error in some “forgotten” class, and it’s not, t possibly with unit tests. Or am I wrong?

2) When you write ridicule and the correct expectations of calling methods, their input data and return values, you need to know how the sub-test class will be implemented. And I think this contradicts test-based development. In TDD, tests are written first, and, managed by them, one writes the code. But I cannot write the correct expectations (and tests in general) unless I write code that needs to be tested. This is contrary to TDD, right?

Both of these problems can be solved if I used real objects instead of mocks. But this is not unit testing, right? In unit test, the sub-test class must be isolated from the rest of the system, not using real classes, but mocks.

I’m sure that I’m wrong somewhere, but I can’t find where. I read and read, and I cannot find what I misunderstood.

+6
source share
5 answers

Strictly speaking, you spend more time writing a unit test, rather than the actual code.

you cannot reach 100% code coverage. I unit test my application, but I feel it is not worth it. But not everyone agrees with him.

If you change the code fragment, your unit test will fail and find out why it fails, but for me it is not worth it.

I am not a big fan of unit testing ... You are mocking at everything except ridicule, this is a big task ..........

But now companies are trying to use the TDD approach.

+4
source

I just implemented TDD and unit testing for the first time. I think this is great, and I never used it in an adjacent integration environment where I imagine it is even more valuable.

My process:

  • Create a skeleton of the class in which the business logic will be maintained (be it a Serivce layer or a domain object, etc.).
  • Write a test and define the method names and the required functionality in the test - which then calls my ide to write the skelton method (a small but nice plus)
  • Then write the actual tested class methods.
  • Running tests, debugging.

Now I can no longer write code without first recording the test, it really helps. You break errors right away, and it really helps you mentally streamline your code and development in a structured, simple way. And, of course, any code that violates existing functionality immediately disappears.

I still did not need to use mock objects (using spring, I can get away with a service level call and directly using the controller methods).

+4
source

The simple answer: Yes, they are useful for the real.

TDD is good and everything is in theory, but I almost never saw how this is done in practice. However, Unittests are not tdd attached. You can always write unittests for code that works to make sure that it still works after the changes. This use of unittests saved me countless hours of fix, because tests immediately indicated what went wrong.

I try to avoid ridicule. In most cases, they are not worth it, in my opinion. Not using mocks may make my unittest a bit more of an integration test, but it does its job. To make sure your classes work as designed.

+3
source

0) No, it is not. Sounds stupid, I mean. This is an absolutely correct question.

The sad fact is that our industry is riddled with silver bullets and snake oil stains, and if something does not make sense, then you are right to ask about it. Any technique, any approach to any part of the technique is applicable only in certain circumstances. As a rule, they are poorly documented at best, and therefore all this tends to worsen to completely meaningless arguments my-method-is-better-than-yours.

I have had successes and failures with unit testing, and, in my experience, it can be very useful if used correctly.

This is not a good technique when you make a huge development attempt from scratch, because everything changes too quickly for unit tests to keep up. This is a great technique when in the maintenance phase, because you are changing a small part of the larger whole, which should still work the way it should.

For me, unit testing is not a development based on testing, but a design contract. unit test verifies that the interface has not been broken. From this it is quite easy to see that the same person should not write a block and its corresponding unit test, so for very small material, like my own animal projects, I never use it. In large projects, I advocate for this, but only if a contract project is used, and the project recognizes interface specifications as important artifacts.

Testing the device as a technique is also sensitive to block size. It is not necessary that the class be tested on its own, and in my experience, the class is usually too small for unit testing. You do not want to test the class as such; you want to test a unit of functionality. Individually deployable (installable) binaries, such as banks or DLLs, make the best candidates in my book or packages if you are in Java.

+1
source

Unit tests themselves are not stupid, it depends on your project.

I think Unit tests are good, not just in TDD development, but in any kind of project. The real professional for me is that if you complete key code in your project with Unit Tests, you can find out if it still works with it or not.

For me, the real problem is that if someone messed up your code, there should be a way to find out if the tests will still succeed, but not force them to run manually. For example, let's look at an example of Eclipse + Java + Maven. If you use unit tests in your Java Maven project, every time someone creates it, tests run automatically. So, if someone messed up your code, then the next time he builds it, he will get a "BUILD FAILED" Console error, indicating that some Unit tests failed.

So, I want to say the following: Unit Tests is good, but people should know that they spin things up without running tests every time they change the code.

+1
source

Source: https://habr.com/ru/post/897887/


All Articles