Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you unit test a unit test? [closed]

I was watching Rob Connerys webcasts on the MVCStoreFront App, and I noticed he was unit testing even the most mundane things, things like:

public Decimal DiscountPrice {    get    {        return this.Price - this.Discount;    } } 

Would have a test like:

[TestMethod] public void Test_DiscountPrice {     Product p = new Product();     p.Price = 100;     p.Discount = 20;     Assert.IsEqual(p.DiscountPrice,80); } 

While, I am all for unit testing, I sometimes wonder if this form of test first development is really beneficial, for example, in a real process, you have 3-4 layers above your code (Business Request, Requirements Document, Architecture Document), where the actual defined business rule (Discount Price is Price - Discount) could be misdefined.

If that's the situation, your unit test means nothing to you.

Additionally, your unit test is another point of failure:

[TestMethod] public void Test_DiscountPrice {     Product p = new Product();     p.Price = 100;     p.Discount = 20;     Assert.IsEqual(p.DiscountPrice,90); } 

Now the test is flawed. Obviously in a simple test, it's no big deal, but say we were testing a complicated business rule. What do we gain here?

Fast forward two years into the application's life, when maintenance developers are maintaining it. Now the business changes its rule, and the test breaks again, some rookie developer then fixes the test incorrectly...we now have another point of failure.

All I see is more possible points of failure, with no real beneficial return, if the discount price is wrong, the test team will still find the issue, how did unit testing save any work?

What am I missing here? Please teach me to love TDD, as I'm having a hard time accepting it as useful so far. I want too, because I want to stay progressive, but it just doesn't make sense to me.

EDIT: A couple people keep mentioned that testing helps enforce the spec. It has been my experience that the spec has been wrong as well, more often than not, but maybe I'm doomed to work in an organization where the specs are written by people who shouldn't be writing specs.

like image 491
FlySwat Avatar asked Oct 28 '08 18:10

FlySwat


People also ask

How do I test a swift closure?

Basically to test asynchronous operation/closure you must: create an expectation that is an instance of XCTestExpectation. execute your closure, make your assert on the closure return value/parameter and call the method fulfill of XCTestExpectation.

What are the three steps in a unit test?

The idea is to develop a unit test by following these 3 simple steps: Arrange – setup the testing objects and prepare the prerequisites for your test. Act – perform the actual work of the test. Assert – verify the result.

What Cannot be unit tested?

Things which truly can't be unit tested: Infinite loops (for a thread manager, driver, or some other type of long running code) Certain types of direct assembly operations (which some languages support) Code which requires privileged access (not impossible, just not a good idea)


1 Answers

First, testing is like security -- you can never be 100% sure you've got it, but each layer adds more confidence and a framework for more easily fixing the problems that remain.

Second, you can break tests into subroutines which themselves can then be tested. When you have 20 similar tests, making a (tested) subroutine means your main test is 20 simple invocations of the subroutine which is much more likely to be correct.

Third, some would argue that TDD addresses this concern. That is, if you just write 20 tests and they pass, you're not completely confident that they are actually testing anything. But if each test you wrote initially failed, and then you fixed it, then you're much more confident that it's really testing your code. IMHO this back-and-forth takes more time than it's worth, but it is a process that tries to address your concern.

like image 93
Jason Cohen Avatar answered Oct 05 '22 04:10

Jason Cohen