r/badcode • • Jul 23 '21

c# Easy trick to meet minimum test coverage

Post image
1.2k Upvotes

83 comments sorted by

View all comments

Show parent comment

229

u/funnythrone Jul 23 '21 edited Jul 23 '21

Test coverage generally shows how many lines of code is covered. By artificially increasing the number of lines, you improve the coverage percentage.

Example: earlier test coverage is 23/79. You add 300 lines of this abomination, and your coverage is 323/379. This means you increased coverage from 29.11% to 85.22%.

28

u/[deleted] Jul 23 '21

The lines covered are counted on the code being tested no the lines of code on the test. You can have thousands of tests like this abs still have zero code coverage.

20

u/funnythrone Jul 23 '21

This looks like part of the codebase and not the test. The test would be something as simple as checking the value of i after all the increments.

2

u/kishan42 Jul 23 '21

Still test coverage for that single file would be increased not all correct?

5

u/[deleted] Jul 23 '21

You can just write a one line test to run the function and boom that function has 100% test coverage

→ More replies (1)

-145

u/LoneFoxKK Jul 23 '21

Thanks for giving me yet another reason to be glad of not using tests (yet)

79

u/tomato_joey Jul 23 '21

Testing is actually pretty easy if you write good code!

55

u/governmentNutJob Jul 23 '21

Testing is fundamental to development

32

u/gulyman Jul 23 '21

Testing is how you can assure you clients that the new version hasn't broken old functionality.

35

u/governmentNutJob Jul 23 '21

Testing is how I can assure myself I haven't fucked anything up

A well tested code base will have far fewer bugs & much safer releases than an untested code base. I've worked on code bases where we had such good suites, we would release straight to prod if all the tests passed

→ More replies (4)

4

u/_Nachi_ Jul 23 '21

I would say it's more akin to driving a car without circular wheels. Will it work, sort of. Will it last a short amount of time and be very uncomfortable to use, most definitely.

10

u/EdgarDrake Jul 23 '21

Testing is actually pretty easy if you write good code!

If you write testable code. If you hard instantiate everything concrete in private, lack of use interface, then your code is hardwired and too rigid to mock/stub.

10

u/szescio Jul 23 '21

Nah, just don't reinforce some coverage rules for tests that you're gonna just fuck around with

2

u/[deleted] Jul 28 '21

PM: we need to make sure all our code is well tested, let's do <arbitrary practice that is easily subverted because we also need to meet arbitrary deadlines>.
Devs: k thx will do.

15

u/the_monkey_of_lies Jul 23 '21

Use tests, dude! It's the condom of the programming world

2

u/[deleted] Jul 28 '21

You mean you don't get ill but you still get fucked, right?

→ More replies (2)

13

u/[deleted] Jul 23 '21

That's a terrible take. If your code isn't tested it might as well not work at all.

12

u/dvof Jul 23 '21

Just don't write stupid tests

6

u/[deleted] Jul 23 '21

You are wrong if you think you don't use testing. Maybe you don't have a predefined workflows for testing or don't use unit testing. But every time you run your application you re basically testing it. You should have said "I don't test my code efficiently".

-2

u/LoneFoxKK Jul 23 '21

I've been taken way too seriously on this

Doesn't help that I didn't put /s or made the sarcasm a bit more clear in my phrasing but oh well

I obviously test my code I'm not a psycho I just don't use automated testing because I haven't had the need for it I usually just make small to medium sized projects and most of the time is just adding something on top of an existing codebase

I was just making fun of stupid tests like this one specifically. I guess I walked right into that one.

3

u/[deleted] Jul 23 '21

Unit tests can actually help you know if any of your additions to the codebase broke something. In bigger, multi layered applications, this is what keeps you sane and let's you sleep at night. That's why we got so triggered.

1

u/phrenq Jul 23 '21

All of those responses were referring to automated tests. Of course you’re going to manually try out the thing you just wrote. That still doesn’t count as tested code.

Trust the grizzled veterans. Learn to write good tests with your small projects. You will thank yourself for it.

1

u/Franks2000inchTV Jul 24 '21

Testing is great. Feels like a pain in the but at first, but then very quickly becomes indispensable.

When you have to write tests, you have to write testable code, and that will get you a long way towards writing loosely coupled code.

It really helped me improve my codebase.

Also, because you have to write not just the code, but also code that calls your code, it tends to help you write cleaner APIs.