Easy trick to meet minimum test coverage

Posted Jul 23, 2021, 4:26 AM UTC

Posted Jul 23, 2021, 4:26 AM UTC
Image Transcription: Code
[This code screenshot begins at line 231 and ends at line 315. It may continue after the 315 mark.]
public static void TestCoverage() {
Integer i = 0;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
^^I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
Imagine if you're blind and the screenreader start reading this
aiplusplusemicolonaiplusplusemicolonaiplusplusemicolonaiplusplusemicolonaiplusplusemicolonaiplusplusemicolonaiplusplusemicolonaiplusplusemicolon...
[deleted]
It's like on twitch when they make the voice to text say L L L L L L L L L L L L L L L L L
[deleted]
Serious question, are there many programmers that are blind?
Like a backend dev I could see, but front end seems like it would be pretty difficult.
It couls be people that are not blind, but are just not good at reading from images. It could also just for saving data :p
There's a TED talk where a blind programmer demo's his setup system that's cool as hell!
Wow, that is impressive. Quite insane to have to work like that.
Good human.
I think you undervalue your time brother.
Dude I feel sad for you
copy paste is my friend
I'm interested in how you do this now, do you hand copy the code from the picture into the comment box when you're transcribing posts?
Yep! I type it into an IDE, add an extra indent to agree with Reddit’s weird Markdown formatting, and then plug that into the r/TranscribersofReddit official format. :)
I've seen this sort of thing where people have just written tests that don't actually test anything for existing functionality just to make it look like we test things.
So many tests that are something ridiculous like
/**
* @TestType=Integration
* @TestCovers=Customer.InitiateCall
**/
void testInitiateCall() {
AssertTrue(true);
}
Never, ever let someone convince you that they've got tests for all their core functionality in an interview while they say they want someone to help clean up some little bits. It's a trap to try and get developers who'd otherwise nope out of a nightmare job before they realise what's happened.
Did they also say they use agile methodology? Haha
What is it for..?
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%.
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.
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.
Still test coverage for that single file would be increased not all correct?
Thanks for giving me yet another reason to be glad of not using tests (yet)
[deleted]
Testing is actually pretty easy if you write good code!
Testing is fundamental to development
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.
[deleted]
Use tests, dude! It's the condom of the programming world
You mean you don't get ill but you still get fucked, right?
That's a terrible take. If your code isn't tested it might as well not work at all.
Just don't write stupid tests
Nah, just don't reinforce some coverage rules for tests that you're gonna just fuck around with
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.
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".
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.
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.
This is what you get when middle management that don't code impose KPI's on developers.
More lines of code doesn't mean more productive. Larger test coverage does not mean fewer bugs.
Well, larger test coverage could mean fewer bugs indeed, couldn't it? I mean of course if you calculate it not like in the meme, then the probability of detecting bugs increases with larger coverage, i.e. the probability of bugs in releases decreases, i.e. there are (likely) fewer bugs
Nevermind, I got the meme wrong - thought, it increases i 100 times and thus returns a coverage of 100% as this thing is called 'TestCoverage'
If you write proper, meaningful tests then yes
And also when you have primadonna developers that are so certain of their own perfection that they decide all policies are bullshit and dont apply to them.
Thus they feel they are free to implement this abomination that belong somewhere in r/maliciouscompliance
Realizing this triggered me more than it should. But wont abort. Let the downvoting begin..
Obviously this only works without branch-coverage targets.
Otherwise you would need
public static void TestCoverage(boolean branch) {
Integer i = 0;
if (branch) {
i++;
}
if (branch) {
i++;
}
if (branch) {
i++;
}
if (branch) {
i++;
}
if (branch) {
i++;
}
if (branch) {
i++;
}
…
I can't tell how much it is r/YandereTechnique even, it's repetitive but there's no if statements...
It isn't done as a mistake, or for lack of knowledge.
This is done deliberately to meet corpo goals for TestCoverage (AKA "tested line count"). The function is brilliantly named TestCoverage, I admire that.
Yeah, you're right.
It still looks funny though
This kind of abomination happens when code coverage becomes a management metric instead of development metric. Sad.
If they were looking ahead to what management might glob onto next, then they also made each line it’s own git commit as well, and the test itself gets longer every rand(5) days or so.
Are you sure that is C#? I have never heard of the type "Integer" and my compiler doesnt recognize it, as in C, its int aka Int32 and long aka Int64. the bracket style is also uncommon for c#. everything points to this being Java.
It looks like Apex to me. Salesforce requires at least 75% test coverage before deploying to production so I could totally see a lazy or cheap firm doing this to artificially inflate the coverage.
You're right, not C#. It's Apex.
I hope the IDE hits the person in the face
Ah yes! Brings me back to the time when I was 11 and a friend showed my how loops work in Commodore 64 BASIC back in 1982. And then later when I discovered unrolling loops in the 90s! A masterpiece of ingenuity.
[deleted]
This is legit all over the code base at my current project. Exactly this, and others like assert true == true, or just plain old commented out unit tests just to pass it.
Indent your code geez
Sure, that's the main problem about this code
Haha yeah not at all 😂
Brainfuck be like
U N R O L L L O O P
[deleted]
I feel like all my code is absolutely awful but holy shit. I’m totally using this at least once at uni just to see the response lol.
Oh no
*heavy breathing
What's the licence on this? Do you mind if I reutilize?
GPL 3.0 - we need to see what you do with this.
Throw it in the the toilet to flush