How to write Test Cases?

cihadakt picture cihadakt · Jan 3, 2013 · Viewed 19.3k times · Source

I want to learn how to write test cases before writing the code. I read an article about test-driven development. I wonder how developers write test cases? For Example this method:

    public int divideNumbers(int num1, int num2)
    {
      return num1 / num2;
    }

Answer

Hosam Aly picture Hosam Aly · Jan 3, 2013

We start with a blank project now. You want to do something, say divide two numbers. So you write a test describing what you want to do:

Assert.That(divide(10,2), Eq(5))

This test gives you an entry point: it describes the acceptable interface of the divide method. So you proceed to implement it as int divide(int x, int y) for example.

Write tests that describe what you expect to get from your code. You don't need to think much about it. The most normal way of writing your expectation is probably the best way to design your code, and then you can implement it to satisfy your test.