Unit Testing for an Absolute Beginner - I'm using Ruby - ruby

I've realised that I really do need to get started on the behaviours and patterns related to unit testing in general and with ruby specifically, as I can then migrate that knowledge to other languages. Are there any really good examples on how to get started? The problem I have is that with the current systems I'm using and working on it seems like getting started with Unit testing is insurmountable.
I've never built a single unit test for operational code other than trying the same fifteen or so online tutorials that show you ruby's core classes seem to function fine. I need to know how to build these tests for my own systems and the mentality of what to look for and test for.
What good tutorials are online that show you how to do more than just test that an assert_true is true and the opposite is false? Even if they're not for ruby, what must-read and must experiment unit testing guide should I read or go over? Preferably one with a step by step tutorial.
Stuff like using these unit tests in actual existing models and frameworks and what to actually test for? I'm still trying to get my head around the testing mentality and I keep getting sidetracked by the different elements. No one's really specifically outlined what mentality you need to approach unit testing with, as all of those who have written guides or tutorials seem to have internalised the logic with which you approach the system.
Any help would be really appreciated.

Technically it's BDD, but I would recommend The RSpec Book because it does a good job of explaining concepts and has extended tutorials. The second section of the book covers Cucumber, so it will teach you that as well.

I know this is a perl tutorial, but I think it's really good for the approach/philosophy: cromatic's and Michael G Schwern's Test::Tutorial pdf. It's the first tutorial I read that said to make your tests fail first, and there's lots of other good tips.

It's really easy to get sidetracked and drawn into details such as code coverage tools like rcov and even more detailed tools such as heckle, flog, flay. These tools are great and they track down bugs but they should be secondary to unit tests being related to design and documentation.
I feel this sums it up - From the agile TDD page
"The act of writing a unit test is more an act of design than of verification."
"It is also more an act of documentation than of verification"

Related

Does Behavior Driven Development just Acceptance testing Software?

I want to know that,does BDD just work in acceptance test level? If not, does it work in unit test level as well? Does BDD have any suggestion For Unit tests?
thank you
BDD is just a way of defining the specifications for an area of functionality. The idea is to bridge the gap between technical and non-technical people by using human readable syntax of some kind and to use specific examples to define the desired behaviour rather than talking in the abstract. As such it is a tool to help people work together and define the business' requirements for the new functionality. This is the primary point of BDD. Not testing.
The definitions that come out of BDD are however, useful for acceptance testing as they define the expected behaviour that was agreed upon. As such many great tools like cucumber are available to facilitate automation of these scenarios to cut down on your testing time.
With regard to using BDD for something like unit tests, the idea of using BDD and non-technical descriptions is to help involve non-technical people. If there are no non technical people involved in the creation of your unit tests (which I guess is the most likely case) then why bother with it? Technical people can read unit tests that are properly written, just fine. The unit tests that you're writing will come out of the functionality that is described by your BDD scenarios anyway.
However, if there is some technical detail of what you're working on that you're having trouble describing, and your team are comfortable working in a BDD way then definitely give the non-technical language and specific examples approach a go. I just wouldn't bother using the the human readable language version of the example in your unit test.
Edit: After reading xmojmr 's comment about your question, I can absolutely see the benefit of using BDD tools and syntax to make your unit tests more readable/easier to plan out, but I think this is quite different from BDD in general, which is more about bridging a communication gap.
BDD actually started at the class level. The first incarnation of JBehave was a replacement for JUnit that avoided the word "test". It was only later that the system-level stuff came along after Dan North explained mock objects to Chris Matts, an analyst at the time who was learning how to code.
These days, even unit testing frameworks don't insist that you start your test methods with the word "test", and the frameworks for the dynamic languages are pretty much derived from RSpec, which was a port of JBehave's early functionality to Ruby anyway.
So, yes, it's perfectly possible to do BDD at that level.
Of course, alannichols is right in saying that the audience is different, being non-technical.
So why would you want to do BDD?
As Dan says in that first link, it turns out that talking about behaviour is more useful than talking about tests. In BDD, we just avoid the word test. We prefer talking about examples and how things should behave rather than pass or fail.
By having conversations around the desired behaviour of a system or class, and providing examples of that behaviour, you can explore it more easily than when you're talking about testing.
However, because it's a non-technical audience, I find it's enough to put the "given, when, then" in comments. You can see an example here. You don't need an explicit BDD tool.
If you can't find another dev to talk about the behaviour with, I suggest you find a rubber duck.

How to learn TDD with Ruby?

I have been using ruby exclusively for about a month and I really love it. However, I am having an incredibly hard time using, or even learning TDD. My brain just doesn't function that way...
I really, really want to learn TDD but to be honest I am a bit confused. All the articles that I find when Googling around are mostly specific to Rails, which is not interesting to me because I want to learn how to do efficient testing for any ruby application, from the simple one-file script to the complicated gem, not for Web apps. Also, there are so many frameworks and so few tutorials to get started.
Could anybody give me any advice on how to learn TDD so that I can at least start to consider myself an aspiring rubyist?
Try RubyKoans.
The best way to learn TDD is by just doing it. I suggest you build a new project using TDD. This means don't write any non-testing code unless you have a failing test.
It will make you think about writing tests: I want to write this code, how do I write a test for it so I can write it.
It will show you the layered nature of testing. Instead of want a name that is required and can't contain numbers. you'll first test setting and reading a name, test requiring the name, test it shouldn't contain numbers, than think if it has more constraints and test those.
Remember:
Write a test before you write the code
Make sure the test fails! It's important to know you're testing logic is correct
Before writing the next test make sure all tests succeed
You can always clean up your code, if the tests keep working you didn't change the design
It's tricky getting your head around TDD (and BDD) but the book RSpec Book - BDD helped me a lot. Behaviour driven development is not exactly the same thing as TDD, but it is close and you have to think in a similar way.
One test at a time. That's the only secret, the rest is just practice, although you'll need to do a lot of practice ;-)
You already have enough to get started: Ruby has the test/unit library, which is quite sufficient to start with.
Try Googling for Ruby test/unit and Ruby TDD. A couple of useful starting points I found are here and here.
I'd start the practice with a completely new project, preferably a sideline one where you're able to progress slower at first. Be really strict - all code should be written as a result of a failing test. Remember the third part of the "red-green-refactor" mantra - you'll soon get into trouble without it (trust me, I've been there).
Once you feel you're starting to become comfortable with the technique (one sign may be that you notice that you don't really have a code-test-debug cycle any more) then start looking at some alternatives: rspec is the main one, but there are others (riot, minitest is the Ruby 1.9 default, if you're on 1.8.x)
I still recommend TDD by Example by Kent Beck. It's an easy read and gives you all the basics.
I personally found the Peepcode RSpec screencast very helpful, they give you a nice idea of what to test plus they get you started with RSpec quickly. It took me a while to get started on TDD, but it's worth it!
I agree with the answers about reading "TDD by Example" by Kent Beck and about having a real project where you force yourself to do it.
You may also find Back to Basics: Writing Unit Tests First useful as a Ruby-related reference.

Is the Create Unit Tests function in Visual Studio worth using on an existing codebase?

In my shop, there isn't much familiarity with unit testing in general, but I'd like to get into it, at the very least, as a proof of concept. Would it be a crazy idea to simply use the Visual Studio function Create Unit Tests on my small (~500 lines) code base and show the concept that way?
Not a crazy idea at all, just know what you should expect to get from that approach.
If you just want to show how to unit test existing code, which is what Roy Osherove calls TAD (Test After Development), it is a quick and easy way to show what kind of things should be tested.
If you are unit testing because you are interested in moving towards TDD, then your approach may serve to confuse, rather than enlighten. Trying to show someone TDD by adding unit tests to existing code defeats the purpose, in my opinion.
Rather, I would look into something (like SpecFlow) that attempts to focus on the spirit of TDD, which is that you're writing a specification (or requirement) in your test code, before you write the real code to make it work.
A light switch flipped for me when I realized that a unit test written before code could actually be my very detailed requirement. It was self-documenting, and as long as the unit test (spec) was written correctly, then my working code was guaranteed to meet the requirement!
If you agree with this, you may want to take a look at the Bowling Game Kata. I found it very helpful for introducing unit testing and TDD concepts.
No this is not crazy at all and is IMHO a great idea.
The only way a group or company can get into a culture of Unit Testing is if someone starts using it. If you don't it's unlikely that the existing people who aren't familiar with the concept will.
A potentially even better idea would be to grab a couple bugs on your plate and write the unit tests which reproduce the failure. Demo the tests catching the real bug and validating the solution. I find that's often fairly compelling for people who aren't familiar with the concept of unit testing.

When should I opt for test driven development?

My question is same as the title. I have not done Test driven development yet,I have performed unit testing and I am aware of testing in general but I hear it is much advantageous to follow TDD?. So should I opt for it everytime or are there some preconditions... i just need to know some basic or important requirements when i should opt for TDD. Sorry if this question is too trivial.
I would say whenever you are coding for a project. By this I mean where you are hoping to produce code that will be used by people. This is basically all code apart from research where you are learning and discovering new techniques.
Even if you think the project is just a small one often things spiral up out of control without care. You wil be glad for the tests when you find yourself having to refactor a big sprawl of code.
Also note that tdd isn't just about testing. It is a methodology of development that encourages you to create clean and solid designs.
As you are starting out tdd everything. Once you have more experience then perhaps you can back off and determine when to not tdd.
IMHO if you are not comfortable with TDD, trying to apply it in projects where you will need to interact/use legacy code it's much more complex than applying it in a project from scratch. In fact, there is a whole book about this.
HTH
My advice would be to use unit testing as often as possible.
Caveat: In my experience TDD works best when working with technologies you already have some experience with. It's often hard to write test assertions if you do not know exactly what the desired result looks like (For example, try writing the test for an ASP.NET MVC action method if you never wrote an action method in your entire life). In those cases you're probably better off writing unit tests after implementing the code.
If I am developing something without a user interface, I always use TDD these days. After all, you have to test the software. You can either do the extra work and do TDD, or you can do the extra work and cludge together a user client just for testing. The former tests more completely and in a more repeatable fashion.
Doing TDD against user interface code, on the other hand, hasn't really delivered much value for me. For various business reasons I'm restricted to Visual Studio out of the box for my work, and "recording" tests with VS is a huge time sink, especially when you have to re-record them if you change the UI. I do TDD for the business logic behind the UI, but not the UI itself.

How do I start unit testing?

I know that unit testing is desirable, and I am interested in doing unit testing. The only problem is I have no idea how, or even where to start really. So my question is: How do I learn about and start unit testing? Specifically I frequently write Java code in NetBeans and C# code in Visual Studio and am interested in what tools to use and how to get my feet wet. Can anyone offer any advice for an absolute unit testing n00b?
I know that there are a lot of somewhat similar questions out there but I am less interested in why and more interested in how.
Try to read on StackOverflow, tag unit-testing :)
Is Unit Testing worth the effort?
How to make junior programmers write tests?
What is unit testing?
How do you know what to test when writing unit tests?
Another entry point would be the tags junit and nunit
There are lots of question dealing this.
If you're searching books about Unit Testing, try this thread:
Good C# Unit testing book. There the famous Kent Beck book is mentioned, "Test Driven Development By Example".
It's worth reading!
Good luck!
This Tutorial for writing JUnit tests in NetBeans should give you an idea how unit testing is done technically. NUnit for C# works pretty much the same.
For an advanced view of how to integrate unit testing into you daily development, the standard reference is Kent Beck's "Test Driven Development By Example". Here's a broad overview.
If you really want to understand unit testing (and get hooked), try it out, it should only take a few hours!
First, I recommend downloading a unit testing framework, such as NUnit (if you want to start with .NET / C#).
Most of these frameworks have online documentation that provides a brief introduction, like the NUnit Quick Start. Read that documentation, then choose a fairly simple, self-contained class for which you are responsible. If you can, try to choose a class that:
Has few or no dependencies on other classes - at least not on complex classes.
Has some behavior: a simple container with a bunch of properties won't really show you much about unit testing.
Try writing some tests to get good coverage on that class, then compile and run the tests.
Unit testing is simple to learn and hard to master (apologies for the cliché, but it is appropriate here), so once you've done this, start reading around: for example, guerda has provided several excellent links in another answer to this question.
Start small.
Unit testing (and automated testing in general) isn't a silver bullet, doesn't always apply to every situation and can be a bit of a culture shock. That said, if you're writing software that you're selling or that your company relies on, I highly recommend adopting it. You'd be surprised how many professional development shops don't.
First, get comfortable the mechanics of creating and running unit tests with your development tools.
Then, start with a new (preferably small, but not necessarily trivial) class or method that you want to test. Writing tests for existing code has its own challenges, which is why you should start with either something brand new or something that you are going to rewrite.
You should notice that making this class or method testable (and therefore reusable) has an impact on how you write the code. You should also find that thinking about how to test the code up front forces you to think about and refine the design now instead of some time down the road "when there's more time". Things as simple as "What should be returned if a bad parameter is passed in?". You should also feel a certain amount of confidence that the code behaves exactly the way you expect it to.
If you see a benefit from this exercise, then build on it and start applying it to other parts of your code. Over time, you'll have confidence in more and more of your software as it becomes more provably correct.
The hands on approach helped get my head around the subject better than a lot of the reading material and helped fill in the gaps of things I just didn't understand. Especially where TDD was concerned. It was counter-intuitive until I actually tried it.
Find-a-bug-write-a-test
The next time you find a bug in your code base, before fixing it, write a test. The test should fail. Then, fix the bug. The test should pass.
If the test doesn't pass, there's either a bug in your test, or a bug in your fix.
A person will never find that bug in your code again. The unit tests will find it (and faster than a person can).
This is definitely a small start, but it gets you into testing. Once you've got the hang of it, you'll probably start writing more tests, and eventually get a knack for how code will fail and which tests you need (for example: a test for every business rule).
Later in your progression you setup a continuous integration server, which makes sure your codebase is always solid.
A good start is to buy a good book where you can read about unit-testing.
I have a tips on a book called "Software testing with visual studio team system 2008" and it takes you trough the basics stuff and background to more higher levels of unit-testing and practises.
Check out The Art of Unit Testing by Roy Osherove, it's a good book for beginners since it starts at the very beginning.
I would recommend reading Michael Feathers' "Working Effectively with Legacy Code". Old code often ends up being code that's hard to Unit Test. Feathers' book is a great guide in how to refactor your code to the point that unit tests are a snap to write.
Not exactly an answer to the question you asked, but might be a missing step between where you are, and where you need to be to implement some of the answers others have given.

Resources