What are some arguments against unit testing

Is unit testing worth it? [closed]


This question does not currently fit our Q&A format. We expect the answers to be supported by fact, testimony, or expertise, but this question is likely to lead to debate, argument, polls, or expanded discussion. If you think this question can be improved and possibly reopened, contact the help center.
Closed 8 years ago.
Blocked . This question and its answers are locked because the question is off-topic but has historical significance. No new responses or interactions are currently being accepted.

I'm working on incorporating unit testing into the development process of the team I work on and there are some skeptics. What are some good ways to convince the skeptical developers on the team of the value of unit testing? In my particular case, we would add unit tests as we add functionality or fix bugs. Unfortunately, our code base is not suitable for simple tests.






Reply:


Every day there is an exchange in our office that goes something like this:

"Man, I just love unit testing, I could just make a number of changes to how something worked and then confirm that I didn't break anything by running the test again ..."

The details change daily, but the feeling doesn't. Unit testing and test-driven development (TDD) have as many hidden and personal benefits as the obvious ones that you can't really explain to someone until they do it themselves.

But if I ignore that, here's my try!

  1. Unit tests are a great way to make big changes to your code quickly. You know it works now because you ran the tests. If you make the changes you need to make, you will need to get the tests back to working. That saves hours.

  2. TDD lets you know when to stop coding. Your tests reassure you that you've done enough for now and can stop optimizing and move on to the next step.

  3. The tests and code work together to make better code. Your code could be bad / buggy. Your TEST could be bad / faulty. In TDD you bet on the probability that both are bad / faulty. Often times the test needs to be fixed, but that's still a good result.

  4. TDD aids in coding for constipation. When faced with a big and daunting job, the tests will make you progress quickly.

  5. Unit tests help you really understand the design of the code you are working on. Instead of writing code to do something, first outline all of the conditions that you will subject the code to and what you would expect it to produce.

  6. Unit tests give you instant visual feedback. We all like the feeling of all these green lights when we're done. It's very satisfying. It's also much easier to see where you left off after a disruption because you can see where you got off to - the next red light to be fixed.

  7. Contrary to popular belief, unit testing doesn't mean writing twice as much code or coding more slowly. It's faster and more robust than coding without testing once you get the hang of it. Test code itself is usually relatively trivial and doesn't add a lot of overhead to what you're doing. This is one that you will only believe if you do :)

  8. I think it was Fowler who said, "Imperfect tests that are done frequently are much better than perfect tests that are not written at all." I interpret this to mean that I have permission to write tests that I think will be most useful, even if the rest of my code coverage is absolutely incomplete.

  9. Good unit tests can help to document and define what something should do

  10. Unit tests help with code reuse. Migrate both your code as well as Your tests in your new project. Adjust the code until the tests are run again.

A lot of the work I have to do with doesn't work well with unit tests (user interactions with web applications etc), but even so, we're all test-infected in this shop and we're happiest when we've bound our tests. I cannot recommend the approach enough.




Unit tests are similar to going to the gym. You know it's good for you, all arguments make sense, so start exercising. There is an initial rush which is great but after a few days one wonders if it is worth the effort. You take an hour to change clothes and run a hamster wheel and you're not sure if you're really gaining anything other than sore legs and arms.

Then, after maybe a week or two, when the pain goes away, a big deadline is approaching. You need to spend every waking hour doing "useful" work so that you can cut out superfluous things like going to the gym. You fall out of the habit and when the big deadline is up you are back in first place. If you even manage to get back to the gym, you'll feel just as sore as you did the first time.

You're reading something to see if you're doing something wrong. You feel a little irrational in spite of all the healthy, happy people extolling the virtues of exercise. You realize that you don't have much in common. You don't have to drive 15 minutes out of your way to hit the gym. There's one in their building. You don't have to argue with anyone about the benefits of exercise. it's just something everyone does and accepts as important. When a big deadline is approaching, they are not being told that exercise is no more unnecessary than your boss would ask you to stop eating.

Unit testing is usually worthwhile to answer your question, but the effort is not the same for everyone. Unit testing can be a huge hassle when you're working with spaghetti codebase in a company that doesn't know how to code quality really appreciates. (A lot of managers will sing the praises of unit testing, but that doesn't mean they'll stand up for it when it comes down to it.)

If you're trying to introduce unit testing into your work and you're not seeing all the sunshine and rainbows you expected, don't blame yourself. You may need to find a new job to make unit testing really work for you.







The best way to convince ... to find a bug, to write a unit test to fix the bug.

This particular bug is unlikely to ever recur and you can prove it with your test.

If you do this enough, others will quickly understand.







The talking walnut asks:

What are some good ways to convince the skeptical developers on the team of the value of unit testing?

Everyone here is going to pile up plenty of reasons out of the blue as to why unit tests are good. However, I find that often the best way to convince someone is to refer to their reasoning Listen and address them point by point. By listening and helping them verbalize their concerns, you can address each one and potentially translate them into your point of view (or at least leave them without a leg to stand on). Who knows? Perhaps they will convince you why unit tests are not appropriate for your situation. Not likely, but possible. If you publish your arguments against unit testing, we may be able to help you identify the counter arguments.

It is important to hear and understand both sides of the argument. If you try too zealously to unit tests without regard to people's concerns, a religious war will ensue (and probably really worthless unit tests). If you take it slowly and start using it where you see the greatest benefit at the lowest cost, you may be able to demonstrate the value of unit testing and have a better chance of convincing people. I realize this isn't as easy as it sounds - it usually takes some time and careful metrics to make a compelling argument.

Unit testing is like any other tool and should be used in such a way that the benefits (finding bugs) outweigh the costs (the hassle of writing). Don't use them when / where they don't make sense, and remember that they are only part of your arsenal of tools (ex. Inspections, claims, code analyzers, formal methods, etc.). What I tell my developers is this:

  1. You can skip writing a test for a method if you have a good argument as to why it isn't necessary (e.g., too easy to be worth it or too difficult to be worth it) and how the method is otherwise checked (e.g. inspection, allegations, formal methods, interactive / integration tests). You need to take into account that some verifications like inspections and formal evidence are done at a specific point in time and then have to be repeated every time the production code changes, while unit tests and assertions can be used as regression tests (written once and executed repeatedly afterwards) . Sometimes I agree with them, but more often I will discuss whether a method is really too easy or too difficult to test.

    • If a developer argues that a method seems too easy to fail, isn't it worth taking the 60 seconds to create a simple 5-line unit test for it? Those 5 lines of code will run every night (you build every night, right?) For the next year or more and are worth the effort if even once a problem is encountered that may have taken 15 minutes or more to identify and debugging. Also, writing the simple unit tests increases the number of unit tests, which makes the developer look good.

    • On the other hand, if a developer argues that a method seems too difficult to test (which is not worth the considerable effort), this may be a good indication that the method needs to be broken up or revised to test the simple parts. Typically, these are methods that rely on unusual resources such as singletons, the current time, or external resources such as a database result set. These methods usually need to be refactored into a method that gets the resource (for example, calls getTime ()) and a method that takes the resource as an argument (for example, uses the timestamp as a parameter). I'll let them skip testing the method that is used to get the resource and instead they unit test the method that the resource is now using as an argument. Typically, this makes writing the unit test much easier and is therefore worth writing.

  2. The developer has to "draw a line in the sand" about how comprehensive his unit tests should be. If we find a bug later in development, they should determine if more extensive unit tests would have identified the problem. If they do and such errors occur repeatedly, they will need to move the "line" to write more comprehensive unit tests in the future (starting with adding or expanding the unit test for the current error). You have to find the right balance.

It is important to realize the unit tests are not a panacea, and it does is something like too much unit testing. When we learn lessons in my workplace, I inevitably hear "we need to do more unit tests". The management nods in agreement because "Unit Tests" == "good" were hit in the head.

However, we need to understand the implications of "more unit testing". A developer can only write ~ N lines of code per week, and you need to figure out what percentage of that code should be unit test code versus production code. A loose workplace may have 10% of the code as unit tests and 90% of the code as production code, resulting in a product with many (if very buggy) features (think MS Word). On the other hand, a strict store of 90% unit testing and 10% production code will have a perfectly solid product with very few features (think "vi"). You may never hear reports of the latter product crashing, but that probably has as much to do with the product not selling very well as it has to do with the quality of the code.

Worse still, perhaps the only certainty in software development is that "change is inevitable". Suppose the strict shop (90% unit tests / 10% production code) creates a product with exactly 2 characteristics (assuming that 5% of the production code == 1 characteristic). If the customer comes by and changes any of the features, that change will discard 50% of the code (45% of the unit tests and 5% of the production code). The lax store (10% unit tests / 90% production code) has a product with 18 features, none of which work very well. Your customer completely revises the requirements for 4 of his functions. Although the change is four times as large, only half of the code base is discarded (~ 25% = ~ 4.4% unit tests + 20% of the production code).

My point is that you need to communicate that you understand the balance between too little and too much unit testing - essentially, that you have considered both sides of the problem. If you can convince your co-workers and / or your management, you will gain credibility and possibly have a better chance of winning them over.







I've played with unit tests several times and I still believe it's worth the effort given my situation.

I develop websites where a lot of the logic involves creating, getting, or updating data in the database. When I tried to "mock" the database for unit testing purposes, it got very messy and seemed a bit pointless.

When I've been writing unit tests around business logic, it has never really helped me in the long run. Since I only work on projects for the most part, I tend to intuitively know which areas of code may be affected by something I'm working on, and I test those areas manually. I want to get a solution to my client asap and unit testing often seems like a waste of time. I list manual tests, go through them myself, and tick them as I walk.

I can see that having one development team working on a project and updating each other's code can be useful, but even then I think that good communication and well-written code should often be enough if the developers are of high quality.




One of the great things about unit testing is that it acts as documentation for how your code is behaving. Good tests are like a reference implementation, and teammates can take a look at them to see how they can integrate their code with yours.


Unit tests are well worth the initial investment. Since I started using unit tests a few years ago, I've found some real advantages:

  • Through regression tests the fear of changes to the code is eliminated (there is nothing like the warm glow when code works or explodes with every change).
  • executable code samples for other team members (and yourself in six months ..)
  • merciless Refactoring - it's incredibly worthwhile, give it a try!

Snippets of code can be of great help in reducing the overhead of creating tests. It's not difficult to create snippets that can be used to create a class map and associated unit test device in seconds.


You should test as little as possible!

That is, you should write just enough unit tests to reveal the intent. This is often glossed over. Unit tests cost you. When you need to make changes and change tests, you are less agile. Keep unit tests short and sweet. Then they have a lot of value.

Too often I see a lot of tests that will never break, are big, clumsy, and don't offer much value. They just slow you down.


I didn't see this in any of the other answers, but one thing that caught my eye is that I can debug so much faster . You don't have to go through your app with the correct sequence of steps to get to the code you want to fix, just find that you made a Boolean mistake and need to run it all over again. A unit test lets you go straight into the code that you are debugging.


[I have to point out that I can't see anything above]

"Every unit test, you don't necessarily notice it - FACT"

Think about it, you are writing a function to possibly parse a string and remove new line characters. As a newbie, you either do some cases from the command line by implementing it in Main (), or you knock together a visual front end with a button, tie your function to a couple of text boxes and a button, and see what happens.

This is unit testing - simple and poorly put together, but you test the code for a few cases.

You're writing something more complex. It throws errors when you throw some cases (unit tests) and debug and trace the code. They look at values ​​as they go through and decide whether they are right or wrong. This is a unit test to some extent.

Unit tests here really pick up this behavior, formalize it into a structured pattern, and save it so you can easily run these tests again. If you write a "real" unit test case instead of manually testing it, it will take as long or less than what you learned, and you will have it available to repeat over and over again


I've been trying for years to convince people that they need to unit test their code. Regardless of whether they wrote the tests first (as in TDD) or after they coded the functionality, I've always tried to explain to them all the benefits of unit testing for code. Hardly anyone disagreed with me. You may disagree with something unobvious, and any smart person will see the benefits of unit testing and TDD.

The problem with testing units is that behavior change is required and it is very difficult to change people's behavior. With words, you will get a lot of people to agree with you, but you won't see a lot of changes in the way they do things.

You have to convince people of that. Your personal success will attract more people than any arguments you may have. When they see that you are not just talking about unit test or TDD but doing what you preach and you are successful, people will try to imitate you.

You should also play a major role as no one will write a unit test correctly the first time. Therefore, you may need to coach him, show him the way and the tools available to him. Help them as they write their first tests, review the tests they write themselves and show them the tricks, idioms and patterns you have learned through your own experiences. After a while, they will see the benefits for themselves and change their behavior to include unit tests or TDD in their toolbox.

Changes won't happen overnight, but with a little patience you can achieve your goal.


An essential part of test-driven development that is often glossed over is writing testable code. At first glance, it might seem like some kind of trade-off, but you'll find that testable code is ultimately modular, maintainable, and readable too. If you still need help convincing people, this is a nice, simple presentation on the benefits of unit testing.


If your existing code base is unsuitable for unit testing and is already in production, you can potentially cause more problems than you can solve by trying to redesign all of your code so that it is unit testable.

Instead, you might want to make efforts to improve your integration tests. There are many codes out there that are easier to write without a unit test. If quality assurance can check functionality using a requirements document, you are done. Send it.

The classic example of this, in my opinion, is a SqlDataReader embedded in an ASPX page that is linked to a GridView. The code is all in the ASPX file. The SQL is in a stored procedure. What are you doing unit test? If the page is doing what it's supposed to do, should you really refactor it into multiple layers so you need to automate something?


One of the best things about unit testing is that it makes your code easier to test. Existing code that was created without testing is always a challenge, as there is often a degree of coupling between classes and difficult-to-configure objects in your class - like sending an email service reference - and so on. But don't let yourself be rushed! You will find that your overall code design will get better once you start writing unit tests. The more you test, the more confident you'll become of making even more changes to it without worrying about damaging your application or causing errors.

There are several reasons to unit test your code. However, over time, you will find that the time you save testing is one of the best reasons for this. In one system that I just shipped, I insisted on doing automated unit tests, although it was claimed that I would spend a lot more time testing than manually testing the system. After all of my unit tests were done, I ran 400+ test cases in less than 10 minutes. Every time I had to make a small change to the code, I only had to make ten changes to make sure the code was still working without errors. Can you imagine how much time it would take to run these 400+ test cases by hand?

When it comes to automated testing - be it unit testing or acceptance testing - it is a wasted effort for anyone to code what you can do manually, and sometimes it's true - if you only plan to run your tests once. The best part of automated testing is that you can run it multiple times with no effort. After the second or third run, the time and effort is yours have wasted already paid.

One final piece of advice would be to not just test your code, but to start testing first (see TDD and BDD for more information).


Unit tests are also particularly useful when it comes to redesigning or rewriting code. If you have good unit test coverage, you can be sure to refactor it. Without unit testing, it's often difficult to make sure you haven't broken anything.


In short - yes. They are worth every ounce of effort ... up to a point. Tests are ultimately still code, and much like typical code growth, your tests may need to be reworked to be maintainable and sustainable. There are a ton of GOTCHAS! When it comes to unit testing, man, man, man, nothing, and I mean, NOTHING enables a developer to make changes more securely than a multitude of unit tests.

I'm working on a project right now ... it's a bit of TDD, and we've grouped the bulk of our business rules together as tests ... we have around 500 unit tests right now. In the previous iteration, I had to revise our data source and the interface of our desktop application to this data source. It took me a couple of days, the whole time just doing unit tests to see what I broke and fixed. Make a change; Build and run your tests. Fix what you broke. Wash, rinse, repeat as needed. What traditionally would have cost days of quality assurance and boat stress was instead a short and enjoyable experience.

Prepare in advance, a little more effort, and it will pay off ten times later when you need to start digging into the core features / functions.

I bought this book - it's a bible with xUnit testing knowledge - it's probably one of the most referenced books on my shelf and I consult it daily: link text



Occasionally, either myself or one of my co-workers will spend a few hours investigating a slightly obscure bug, and once the root cause of the bug is found, 90% of the time the code will not be tested for integrity. The unit test does not exist because the developer compromises due to time constraints, but then loses this and more debugging.

Spending little time writing a unit test can save you hours debugging in the future.



I work as a maintenance technician with a poorly documented, terrible, and large code base. I wish the people who wrote the code wrote the unit tests for it.
Every time I make a change and update the production code, I'm afraid that I could introduce a bug because I didn't take a condition into account.
If they wrote the test, changes to the code base would be easier and faster (at the same time, the code base would be in better shape).

I think unit testing comes in very useful when you are writing APIs or frameworks that will last for many years and need to be used / modified / developed by people other than the original coders.



Unit testing is definitely worth the effort. Unfortunately, you've picked a difficult (but unfortunately common) scenario that you want to implement it in.

The best advantage of unit testing is that you can use it from scratch. On a few selected, small projects, I was lucky enough to write my unit tests before implementing my classes (the UI was already completely point). With proper unit testing, you will find and fix bugs in your classes while they are still in their infancy, rather than near the complex system into which they will undoubtedly be incorporated in the future.

If your software is solidly object-oriented, you should be able to add class-level unit tests with little effort. If you're not so lucky, you should still try to incorporate unit testing wherever you can. As you add new functionality, make sure that the new parts are well defined with clear interfaces and that unit testing makes your life a lot easier.


If you said, "Our code base is not designed for simple testing," that is the first sign of a code smell. Writing unit tests means that you usually write code differently to make the code more testable. This is a good thing in my opinion, as over the years I've had to come up with a better design writing code that I had to write tests for.


I do not know. Many places don't do unit tests, but the quality of the code is good. Microsoft is doing unit tests, but Bill Gates gave a blue screen on his presentation.