views:

44

answers:

6

Test driven development on wikipedia says first develop a test that will fail because the feature does not exist. Then build the code to pass the test. What does this test look like?

How do you figure out what test will best represent the feature you want to create?

Can someone give an example?

Like if I make a logout button feature to a web application then would the test be hitting the page looking for the button? or what?

I heard test driven is nice for regression testing, I just don't know how to start integrating it with my work.

A: 

the unit test would be calling the logout function and verifying that the expected results occurred (user login record ended, for example)

clicking the logout button would be more like an acceptance test - which is also a good thing to do, and (in my opinion) well within the scope of TDD, but it tests TWO features: the button, and the resulting action

Steven A. Lowe
With ASP.NET WebForms I've used Selenium to do what in essence is TDD, because mocking the HTTP State is so difficult that in the end doing functional scripted testing is about as close as I can get to real TDD. It's not ideal however.
Nissan Fan
A: 

The test would be making sure that when the logout function was executed, the user was successfully logged out. Generally a unit testing framework such as NUnit or MSTest (for .Net stuff) would be used.

Web applications are notoriously hard to unit test because of all the contextual information generally required for the execution of server code on a web server. However, a typical example would mock up that information and call the logout logic, and then verify that the correct result was returned. A loose example is an MVC type test using NUnit and Moq:

[Test]
public void LogoutActionShouldLogTheUserOut()
{
    var mockController = new Mock<HomeController>() { CallBase = true };
    var result = mockController.Object.Logout() as ViewResult;

    Assert.That(result.ViewName == "LogoutSuccess", 
                "Logout function did not return logout view!");
}

This is a loose example because really it's just testing that the "LogoutSuccess" view was returned, and not that any logout logic was executed. In a real test I would mock an HttpContext and ensure the session was cleared or whatever, but I just copied this ;)

Unit tests would not be testing that a UI element was properly wired up to an event handler. If you wanted to ensure that the whole application was working from top to bottom, this would be called integration testing, and you would use something besides unit tests for this. Tools such as Selenium are commonly used for web integration tests, whereas macro recording programs are often used for desktop applications.

womp
A: 

It depends on what platform you are using as to how your tests would appear. TDD is much harder in ASP.NET WebForms than ASP.NET MVC because it's very difficult to mock up the HTTP environment in WebForms to get the expected state of Session, Application, ViewState etc. as opposed to ASP.NET MVC.

A typical test is built around Arrange Act Assert.

// Arrange
... setup needed elements for this atomic test

// Act
... set values and/or call methods

// Assert
... test a single expected outcome

It's very difficult to give deeper examples unless you let us know the platform you plan to code with. Please give us more information.

Nissan Fan
So with .NET MVC how would you make a call to an action and consume the results in a testing class (C#) instead of through the HTTP+HTML?
shogun
Other than this example does multiple Assertations (I'm a 1 Test, 1 Assert guy), it's a good example of doing exactly what you're requesting using ASP.NET MVC: http://weblogs.asp.net/shijuvarghese/archive/2009/07/22/introduction-to-test-driven-development-with-asp-net-mvc.aspx
Nissan Fan
A: 

Say I want to make a function that will add one to a number (really simple example).

First off, write a test that says f(10) == 11, then do one that says f(10) != 10. Then write a function that passes those tests. If you realise the function needs more capabilities, add more tests.

Skilldrick
+1  A: 

Well obviously there are areas that are more suited for TDD than others, and running frontend development is one of the areas that I find difficult to do TDD on. But you can.

You can use WATIN or WebAii to do that kind of test. You could then:

  • Write a test that checks if a button exists on the page ... fail it, then implement it, and pass
  • Write a test that clicks the button, and checks for something to change on the frontend, fail it, implement feature and pass the test.

But normally you would test the logic behind the actions that you do. You would test the logout functionality on your authenticationservice, that is called by your eventhandler in webforms, or the controller actions in MVC.

Luhmann
A: 

What does this test look like?

A test has 3 parts.

  1. it sets up a context
  2. it performs an action
  3. it makes an assertion that the action did what it was supposed to do

How do you figure out what test will best represent the feature you want to create? Tests are not based on features (unless you are talking about a high level framework like cucumber), they are based on "units" of code. Typically a unit is a function, and you will write multiple tests to assert all possible behaviors of that function are working correctly.

Can someone give an example? It really varies based on the framework you use. Personally, my favorite is shoulda, which is an extension to the ruby Test::Unit framework

Here is a shoulda example from the readme. In the case of a BDD framework like this, contextual setup happens in its own block

class UserTest < Test::Unit::TestCase
    context "A User instance" do
      setup do
        @user = User.find(:first)
      end

      should "return its full name" do
        assert_equal 'John Doe', @user.full_name
      end

      context "with a profile" do
        setup do
          @user.profile = Profile.find(:first)
        end

        should "return true when sent #has_profile?" do
          assert @user.has_profile?
        end
      end
    end
  end

Like if I make a logout button feature to a web application then would the test be hitting the page looking for the button? or what?

There are 3 main types of tests.

First you have unit tests (which is what people usually assume you are talking about when you talk about TDD testing). A unit test tests a single unit of work and nothing else. This means that if your method usually hits a database, you make sure that it doesn't actually hit that database for the duration of the test (using a technique called "mocking").

Next, you have integration tests. An integration test usually involves interaction with the infrastructure, and are more "full stack" testing. So from your top level API, if you have an insert method, you would go through the full insert, and then test the resulting data in the database. Because there is more setup in these sorts of tests, they shouldn't really be run from developer machines (it is better to automate these on your build server)

Finally, you have UI testing. This is the most unreliable, and requires a UI scripting framework like Selenium or Waitr to automate clicking around your UI. Don't go crazy with this sort of testing, because these tests are notoriously fragile (a small change can break them), and they wont catch whole classes of issues anyways (like styling).

Matt Briggs