RubyFlow The Ruby and Rails community linklog

test_inline - Put tests next to the code being tested

test_inline is a new experiment in automated testing. The idea is to put your tests next to the code being tested. Sort of like how rdoc brought documentation into the code this does the same for tests. If you feel like trying something new check it out.

Comments

I actually like the idea quite a bit. If your tests are simple enough, it would eliminate the need to even write documentation in some cases. At the same time, I have a hard time seeing how this would work in a complex project. There is so much rspec does that make testing easier, before hooks, etc. Plus I can only image how big files would be with the tests mixed in.

I like the idea too. Great for simple tests. And if your rspec tests are complex you could add a very simple, short, high-level integration test that would run half of the system but would verify it too. And better have fast tests ;)

One nice thing about this approach is that it is not exclusive. It works great on unit testing but obviously on things like integration testing (i.e. things like cucumber) you are crossing lots of methods so there is no obvious place to put it. So back in the test/ directory it goes.

So use it for where it makes sense. Do not use it where it does not make sense.

I actually did this for a long time with Facets. What I did was simply put the test case at the bottom of the file wrapped in a =begin test … =end comment. Then I had a tool that simple extracted the tests and ran them. It worked very well. B/c the test were in comments there was no load hit to the code itself.

But in the end it became clear that it was only an effective strategy for simple programs. Once you move beyond basic unit test then where to put the test became an issue.

When it comes to “testing” in code DBC is more the way to go.

I use rubydoctest.

It’s comments don’t pollute the runtime as test_inline does but still offer great documentation for simple methods.

Some of the issues brought up I have commented about elsewhere so thought I would point them out to those curious.

comment idea.

Performance - Basically this is a non-issue. It doesn’t add anything worth-while to the load time since when you are not testing the Test block just returns. Also if it really bothered you then you could just defined Kernel#Test to be a no-op and not even include the test_inline library except when actually testing.

@Gert Theil - I hadn’t seen rubydoctest. It looks interesting but I prefer using standard Test::Unit style assertions over the irb syntax. That may just be preference. Also you can avoid the “pollution” of the runtime by defining Kernel#Test as a no-op as my comment above suggested.

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in