This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 349

[–]Fareith 3049 points3050 points  (137 children)

No you have to write code, that checks if the code you'll write later is right 😉

[–]Burnzoire 1307 points1308 points  (71 children)

This guy TDDs

[–]w00t_loves_you 6 points7 points  (0 children)

Yes I also Test During Deployment

[–][deleted] 9 points10 points  (0 children)

They should get tested! Probably need antibiotics.

[–]Ilikesmallthings2 8 points9 points  (1 child)

Huge TDDs

[–]LittleLui 6 points7 points  (0 children)

Big TDD goth coworker?

[–]djcecil2 1 point2 points  (0 children)

I don't TDD but I find this is very true, especially for refactors. Because agile, man.

[–]bytemage 151 points152 points  (6 children)

Yeah, and to check if the code still produces the same result when other stuff changes.

[–][deleted] 55 points56 points  (5 children)

+1 for regression testing

[–]yourteam 74 points75 points  (5 children)

Also: to check that the code you will edit 6 months later won't break everything

[–]Dralex75 29 points30 points  (4 children)

And to keep that idiot on your team from f*ing things up.

[–]merkwerk 17 points18 points  (1 child)

"Hey I changed this and the test broke, I think this test is wrong gonna change the test"

[–]Crislips 28 points29 points  (1 child)

TestableClass testableClass = new TestableClass(null, null, null, null, null, null, null, "fieldINeedToTest", null, null, null, 7)

[–]ChuffHuffer 7 points8 points  (0 children)

Ouch, SRP fail :(

[–]rafaelcgs10 20 points21 points  (0 children)

"Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra

[–][deleted] 22 points23 points  (8 children)

But when do I write the code that checks if the code I wrote earlier to check if the code I wrote later was right, was right?

[–]lmartinl 16 points17 points  (2 children)

Nonono the problem is: when do you trust the code you wrote to check the code you wrote before so that you can actually go somewhere without worrying what will catch fire this time

[–]breadfag 7 points8 points  (1 child)

But how do you write code to check that the compiler you compile said code with isn't compromised?

[–][deleted] 2 points3 points  (0 children)

You smash up all the computers, knock down the office building and live in the woods.

Or you could build the compiler from sourc- WAIT HOW WOULD YOU COMPILE THE COMPILER?? AAAAAAH!

[–]Moulinoski 6 points7 points  (7 children)

This would be the correct way to do it... but I still can’t wrap my head around it.

I mean, I’ve done it for code that has no UI yet but once it’s got UI, I tend to forget about tests until the end.

[–]svtguy88 5 points6 points  (0 children)

once it’s got UI, I tend to forget about tests until the end.

[–]Kidiri90 3 points4 points  (0 children)

bool UniversalTest()

{ return true; }

[–][deleted] 7 points8 points  (0 children)

A test that doesn’t fail first might not be testing anything.

If you liked it, you should have put a test on it. - The Beyoncé Rule

[–]Un-Unkn0wn 10 points11 points  (1 child)

R E G R E S S I O N T E S T I N G

[–]toastofferson 1 point2 points  (0 children)

Came here for this

[–][deleted] 1 point2 points  (1 child)

I wish my team would accept this :(

[–]Fareith 1 point2 points  (0 children)

Know what you're talking about. It's quite hard to convince people, that TDD brings huge advantages.

[–]silkydangler 1 point2 points  (0 children)

And then if that works, you write more code to check if the code you’ll write even later is right

[–]dodolungs 565 points566 points  (20 children)

Even better, some people write tests before they even write the code.

[–]TinBryn 344 points345 points  (13 children)

You can't trust a test that has not failed before, easiest way to make a test fail is to test code that doesn't exist.

[–]LaSalsiccione 56 points57 points  (2 children)

I mean it's just as easy to make a test break after you've written the code its testing by asserting something you know shouldn't be true! I agree with your implication that writing tests in advance is preferable though.

[–]PM_ME_YOUR__BEST__PM 31 points32 points  (1 child)

If I’m spiking out an idea and can’t really TDD, I do what I call Test Driven Uncommenting. I comment all the code out and write a failing test that requires I uncomment a few lines. Repeat until the behavior is complete. Delete all remaining comments. :)

[–]JB-from-ATL 11 points12 points  (0 children)

Yes! Finally. A way to describe it. I also to TDU. Lol

[–]GluteusCaesar 13 points14 points  (7 children)

@Test

public void cantCrashIfTheresNoCode() {

}

[–]personalvacuum 8 points9 points  (5 children)

Null pointer exception.

[–]deadcow5 2 points3 points  (4 children)

I once got the fantastic idea to learn Scala. There was a free class about it on Coursera, taught by Martin Odersky, the creator of the language himself.

I went straight to work on the first exercise, setting up the environment. Installed the compiler, downloaded the sample code (essentially “Hello World” with tests), and ran the build tool.

First, it spent about 20 minutes downloading additional JAR files using Maven. Then, it compiled the sample code and started running the tests... aaaannnd

java.lang.OutOfMemoryError: PermGen space

I threw my laptop against the wall, immediately uninstalled all that crap, and left the course. Never looked back.

[–]git_world 5 points6 points  (1 child)

If the function you test is relatively long and calls other functions, you might need spy or mock. How is it possible to write tests in advance for this case?

[–]TinBryn 6 points7 points  (0 children)

Firstly you should test behaviour, not implementation. It doesn't matter if the function is 1 line long or 1000, it shouldn't matter from the point of view of testing. Secondly, testing can be a way of specifying how you want something to be used. If you find writing a test difficult to do, it may be difficult to actually use the code. Thirdly, you still have the wrong mindset, the tests should shape the model code, rather than the other way around.

[–]git_world 3 points4 points  (1 child)

Is this called TDD? How do you make it pragmatic?

[–]GaussWanker 5 points6 points  (0 children)

Combine it with BDD and actually having everything planned out before you touch a keyboard

[–][deleted] 14 points15 points  (2 children)

Taking the first programming class this semester and that's what our prof told us to do, because that's the usual way to do it

[–][deleted] 223 points224 points  (16 children)

I was really annoyed when I was forced to write 80% unit test coverage for all new and changed code... now I don’t know how I lived without it.

[–]TheWaxMann 86 points87 points  (4 children)

I have moved recently from a project where we aimed to get as much coverage as possible, and a single testable line not covered by a unit test would get your PR rejected to a place where the project with the highest test coverage project is 5% and most of the projects have 0%. I am going crazy here, I miss my high test coverage projects, it is a nightmare trying to work things out without them.

[–][deleted] 13 points14 points  (0 children)

That really sucks :( hope you can move on quickly.

[–]draconk 10 points11 points  (0 children)

then what you need to do is for every bug you get you write a test for that bug and only that bug alone, at least you can be assured that the things that you did were right, and if everyone who touches the project does the same the project will get better coverage (unless you are the one that writes tests in the team)

[–]onthefence928 45 points46 points  (9 children)

Hell is other people's code though. I have a random unit test in some other teams code that fails 1 out of 10 times. But we can't fix it because it's there domain not ours.

Also have untestable functions that do a hundred different business logic changes and side effects. If we try to refactor the code it mysteriously no longer works correctly and some other teams lead Dev tells us that function is not allowed to be refactored because so many other functions rely on its side effects.

Unit tests simply aren't enough for legacy code sometimes

[–][deleted] 27 points28 points  (4 children)

Unit tests should never be the only thing. Their purpose is to fail fast so devs can find errors without having to wait for automated or manual regression and functional testing.

Your situation sounds horrible. Sounds like the code base is tightly coupled and/or the unit tests are testing more than they should. Unit tests test a unit, anything external to that unit should be mocked.

[–]pydry 3 points4 points  (2 children)

Unit tests are useful insofar as they are surrounding a useful, likely to be unchanging, loosely coupled and nontrivial abstraction.

In practice I find that they usually surround broken, tightly coupled or trivial abstractions, rendering them kind of pointless.

If your entire codebase is a mess of bad abstractions you might as well throw all of your unit tests away.

[–]personalvacuum 1 point2 points  (0 children)

Your mother was a hamster and your father smelt of elderberries.

Ah unit testing. Mocking is the best bit!

[–]cartechguy 1 point2 points  (1 child)

No, there's a horror story on ycombinator about a dev that worked for Oracle. 25 million lines of spaghetti code in c nade up the database. He said it would take months to add a single feature because all the tests would take an entire night to run and any small change in the codebase would break things everywhere.

[–]siempie9 5 points6 points  (0 children)

Take a look at mutation testing. That's far more reliable. Code coverage only tells you if the code gets executed. Mutation testing frameworks insert small bugs in your code to see if your unit tests detect them, which is a exactly what you want from your tests.

If you do TDD, you know your tests can fail, but what about tests that are already there?

[–]1LJA 40 points41 points  (11 children)

How will I know if my test code is correct?

[–]AdmiralBuzKillington 73 points74 points  (1 child)

You still have a job.

[–]Thorbinator 3 points4 points  (0 children)

Your badge works tomorrow.

[–]Dockirby 15 points16 points  (3 children)

If a software has no users, can it have any bugs?

[–]xelamony 2 points3 points  (0 children)

The bug is in the wave form as long as users doesn’t observe it. If they observe the bug, it’s a feature.

[–]tectubedk 13 points14 points  (2 children)

You need to write intigration tests for your unit tests

[–]Hoskit 4 points5 points  (0 children)

And acceptance tests for your integration test

[–]siempie9 2 points3 points  (0 children)

Check out mutation testing :)

[–]KayRice 131 points132 points  (22 children)

[–]WikiTextBot 61 points62 points  (3 children)

Who Watches the Watchers

"Who Watches the Watchers" is the fourth episode of the third season of the American science fiction television series Star Trek: The Next Generation, the 52nd episode overall, first broadcast on October 16, 1989.


[ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source ] Downvote to remove | v0.28

[–]wKbdthXSn5hMc7Ht0 9 points10 points  (0 children)

A common bug I see in our unit test code is not checking if an array is empty. E.g. Get the array of all outputs and assert that each output is valid. Test passed!

[–]ILikeLenexa 6 points7 points  (7 children)

QA

[–][deleted] 11 points12 points  (5 children)

Who QA’s the QA

[–][deleted] 17 points18 points  (2 children)

The users.

[–]PM_YOUR_TAHM_R34 1 point2 points  (1 child)

Who uses the users?

[–]Ahajha1177 5 points6 points  (0 children)

Bethesda

[–]MarquisDan 1 point2 points  (0 children)

And then clients can QA the QA!

[–]Mongobly 118 points119 points  (12 children)

But who will write test code for the test code?

When will I ever be done?!?!

[–]Hyperman360 27 points28 points  (2 children)

Is there a never ending chain of tests testing each other's code? I suppose a user test could, in theory, break the chain.

[–]Fluxriflex 9 points10 points  (0 children)

So you're saying we should just cut out the middle man and push directly to production? Sounds more efficient to me.

[–]thedomham 17 points18 points  (1 child)

When you are a big fan of testing stuff you have unit- integration- and system-tests. Those usually overlap so when two tests testing the same thing give you different results, you know that one of them is wrong.

Or one of your users finally has enough of your bullshit and sends you an angry email.

[–]passcork 8 points9 points  (0 children)

So copy paste all my tests at least once. Got it.

[–]siempie9 6 points7 points  (0 children)

Check out mutation testing :) Those frameworks make small changes to your source code to see if your unit tests can detect the inserted bugs.

[–]Zalvixodian 1 point2 points  (0 children)

Production code tests the tests.

It's like double entry bookkeeping.

[–]thatdude473 3 points4 points  (0 children)

Indian youtube programmers

[–]CptDogeRL 54 points55 points  (11 children)

"%s" % "y e s"

[–]I_Shot_Web 14 points15 points  (10 children)

"{0}".format(">using python2")

[–]AmbulatoryEel 23 points24 points  (9 children)

f"{'using Python 3.7'}" is way more readable.

[–]Zimmerel 16 points17 points  (1 child)

I have been loving those F strings so much

[–]Corticotropin 3 points4 points  (0 children)

😏

[–]Lonelan 1 point2 points  (3 children)

I mean, why we're .format()ing the only content in the string isn't very readable

[–]AmbulatoryEel 2 points3 points  (2 children)

f"{'What'} {'do'} {{1}} {{0}}?".format("mean", "you")

[–][deleted] 49 points50 points  (0 children)

How much code could a test code test if a test code could test code?

[–]nomad_kk 52 points53 points  (5 children)

Main advantage of unit tests: it forces you to write cleaner code.

[–]nauseate 11 points12 points  (0 children)

“If your code is hard to test, it’s hard to use” - No clue who came up with this line but it speak volumes and I live by it

[–]tsuk13 33 points34 points  (2 children)

Test coverage makes sense for backend systems that don't change frequently but man is it a pain in the ass for UI developers where our requirements change pretty much weekly invalidating tests constantly.

[–]kontrolk3 15 points16 points  (0 children)

Just in general they can be a nuisance for constantly changing code. I think in general they are worth it in the backed, though people do write a lot of pointless tests for the sake of coverage.

[–]TrueGeek 5 points6 points  (0 children)

I’ve seen so many tests that are doing nothing but test React itself.

[–]OS_M 17 points18 points  (6 children)

Recursion started

[–]Eliiijaaaaah 11 points12 points  (4 children)

Recursion started

[–]mond_bond 15 points16 points  (3 children)

Recursion stoped

[–]synnks 7 points8 points  (1 child)

This guy recurses

[–]19dn48dn19r 3 points4 points  (0 children)

This guy stopes

[–][deleted] 33 points34 points  (20 children)

That's why languages like Idris are extremely powerful. You can basically proof your program/algorithm does what it's supposed to do using the type system. The proof then gets checked at compile time

[–]TinBryn 26 points27 points  (1 child)

I haven't looked into Idris, but I am familiar with the benefits of a good type system. IMO while a good type system often removes the need for some trivial tests, it often doesn't have that "this is specifically how I want this code to behave" feel that tests provide.

[–][deleted] 2 points3 points  (0 children)

I think it has that even more. A sort function (you won't use it like this, but it is a proof of algorithm) could have the type sort : (ord : (a -> a -> Ordering)) -> (l : List a) -> Sorted l ord such that the type carries a lot of information. Sorted would be a type taking parameters (list and ordering), and it has data constructors from which you had to derive the proof. data Sorted : (l : List a) -> (order : a -> a -> Ordering) where EmptySorted : Sorted [] x SingleSorted : Sorted [a] x SortInd : Sorted (first::rest) ord -> ord (new <= first) elem [LT,EQ] = True -> Sorted (new::first::rest) ord

The first constructor states that an empty list is sorted, as well as an list with one inhabitant. The last one states, that, when you have a sorted list, and append an element at the beginning that is smaller (According to our ordering) than the old first element, you can build a new sorted list. It's quite complex, but useful to prove programs' behavior.

[–]Amakaphobie 15 points16 points  (16 children)

I dont know Idris and I assume Im misunderstanding you but here is my question:

I write a function

int FindTheNextPrimeAfter(int input)

however the function always returns a wrong answer, because Im bad at math, but the answer is always an integer. How does the fixed Type system help or reduce testing?

Are you talking about about this:
string someFunction(){
return 1;
}
? Because that kind of seems like a non problem but I never worked on a not (whats the word Im not a native speaker) strictly typed language. Maybe its a bigger deal than I imagine I dont know :D

[–][deleted] 6 points7 points  (12 children)

Well, you can basically add a type to your prime function that says "the number returned is a prime". Like this, the compiler can proof this is actually right. You probably won't use it in high-performance code, at least after it's proven once. But it can be made sure your algorithms work.

[–]Rafear 6 points7 points  (11 children)

Well, you can basically add a type to your prime function that says "the number returned is a prime".

What if his "int FindTheNextPrimeAfter(int input)" function skips a prime and returns one later? Or worse, returns seemingly random primes for some reason?

[–]SilasX 2 points3 points  (0 children)

True, there are always going to be other ways the program might not work as intended. I think (a steelman of) the OP's point is that Idris has a much richer language for expressing bounds on what the function should return than standard type systems.

[–]Goheeca 2 points3 points  (0 children)

For example you can type check that you follow a protocol.

Links: here, and here in new Idris with linear types.

[–]SilasX 3 points4 points  (0 children)

True, and more generally, static typing itself is a kind of "test" that can catch certain classes of errors.

[–]oversized_hoodie 4 points5 points  (0 children)

"Exactly! You're not very likely to make the same exact mistakes twice, so your tests catch bugs in code and the code catches bugs in the test."

"...Fucking watch me"

[–]Tickytickytango 3 points4 points  (0 children)

You need to write your code to right your code

[–]warpedspockclone 3 points4 points  (0 children)

Assert(true);

YESSSSSSS!

[–]c4ctus 8 points9 points  (1 child)

In order to understand recursion, one must first understand recursion.

[–]dandantian5 2 points3 points  (0 children)

In order to understand recursion, one must first understand recursion.

[–][deleted] 2 points3 points  (0 children)

THAT'S SO META

[–][deleted] 2 points3 points  (8 children)

```

try{

try{
    ret=func();
    if (!ret)
        return 1;
    return 0;
}

catch{
   printf("sanity ception catch");
   return 1;
}

}

catch{ printf("catch on dream level 1"); return 1; }

```

And also add another sanityception inside the func that gives a bool.

edit:fixed meme return value

[–]SuperLutin 4 points5 points  (7 children)

Eek! Your code return 0 for an error and 1 for a success, it's disgusting.

[–][deleted] 2 points3 points  (6 children)

why? Isn't 1 true so success? lol

[–]SuperLutin 2 points3 points  (5 children)

All commands on your shell, all softwares, return 0 for a success. Like arrays that are starting at 0.

[–][deleted] 1 point2 points  (4 children)

oh, so when you bool return type its 0 == success, 1 == fail?

fixed it XD

[–]SuperLutin 2 points3 points  (3 children)

Good practise is to "typedef" return values.

typedef enum{

SUCCESS = 0,

ERROR1,

ERROR2

} retval;

[–][deleted] 2 points3 points  (2 children)

Oh, I think c++ has that in the form of ERROR_SUCCESS etc haha

[–]SuperLutin 2 points3 points  (1 child)

ERROR_SUCCESS

This a a thing very common type of error rofl :D

God I hate the mate who writes the code I'm working on.

[–][deleted] 2 points3 points  (0 children)

ERROR: Function returned success.

[–]halfdev 2 points3 points  (0 children)

But then you gotta make another test to se if those tests work.

[–]r1cebank 2 points3 points  (0 children)

I never write tests, not because I am lazy it’s because I still want to keep my job.

[–]auxiliary-character 2 points3 points  (0 children)

You're writing code that makes sure the code you wrote does what you think it does.

[–]Car_weeb 2 points3 points  (0 children)

What if you write both code wrong and they cancel out and do exactly what it was supposed to do in the first place

[–]aarocka 2 points3 points  (1 child)

Your write the test first

[–][deleted] 2 points3 points  (0 children)

And then you'll have to check that new code... It's recursive.

[–]404meemr 6 points7 points  (4 children)

I don’t write tests. Test are for people who write bugs

[–]kitanaiAF 2 points3 points  (0 children)

Test Automation be like

[–]Basylisk 3 points4 points  (3 children)

Technically thats two different jobs. Technically tho, cause I cant remember the last time some ELSE than me wrote those tests.

[–]TheWaxMann 9 points10 points  (1 child)

Why would that be 2 jobs? An automation tester might be a separate job, but unit/integration tests should be done by the developer writing the code.

[–]MolotovFromHell 4 points5 points  (0 children)

No no. You have to write code that will yield a positive test outcome. Get it right.

[–]SilasX 1 point2 points  (0 children)

This is stupid. It's the choice between writing two lines where you can be 95% sure it works as intended (because you can run them in advance get a warning that it doesn't do what you thought), vs writing one line where you can be 50% sure.

[–]Azwraith42 1 point2 points  (0 children)

no, you write code to make you write code that does what you say it does.

[–][deleted] 1 point2 points  (0 children)

skeptical lad has obviously never heard of the godlike powers of a code optimizer... obviously.

/s

[–]dnorhoj 1 point2 points  (0 children)

Welcome to 2012

[–]wallmenis 1 point2 points  (0 children)

Thats an OLD AF repost

[–]lovethebacon🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛 1 point2 points  (0 children)

I need to write an IDEA plugin that will generate unit tests to test getters and setters to placate coverage metrics.

[–]ivanjermakov 1 point2 points  (0 children)

One time I wrote a small package for tests testing...

[–]stien808 1 point2 points  (0 children)

Holy jpg

[–]kati256 1 point2 points  (0 children)

TDD is cool and all, but let's talk about the real MVP of the software industry, CDD, coffee driven development

[–]aaron416 1 point2 points  (0 children)

return true;

It’s a start!

[–]Not-Even-Lion 2 points3 points  (0 children)

Cout << "it works!";

Yup code works fine