InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Matrix Your Rails Functional Tests

Posted by Sebastien Auvray on Apr 19, 2007

Sections
Development,
Architecture & Design
Topics
Software Testing ,
Ruby ,
Unit Testing
Tags
ZenTest ,
Testing
Ryan Davis added a new way to test multiple edge cases in his latest test suite release: ZenTest 3.5.0. The way he suggests to make it clearer is by using a matrix. Let's imagine you have rights properties to test in your application (with orthogonal states: readable vs unreadable, ...). You would basically end with the following 4 methods:
def test_edit_user_readable
    some_setup_to_initialize_user_readable_context
    some_action_here_edit
    some_assertion_error_read
end

def test_edit_user_writable
    some_setup_to_initialize_user_writable_context
    some_action_here_edit
    some_assertion_edit
end

def test_view_user_readable
    some_setup_to_initialize_user_readable_context
    some_action_here_view
    some_other_assertion_view
end

def test_view_user_writable
    some_setup_to_initialize_user_writable_context
    some_action_here_view
    some_other_assertion_view
end
You can easily see that some pieces of this code (setups, actions) can be factored. But Ryan went a step further by reorganizing it into a matrix where the column headers represent the different setup cases, the row headers represent the action applied and the intersection defines the expected result for a given action and setup context.

The 4 test cases will look like this:
setups   :user_readable, :user_writable
matrix :edit, :error_read, :edit
matrix :view, :view, :view
ZenTest will stores off the edge case through the setups method and the matrix method will create a test method for every applicable result:
def test_#{action}_#{setup}
    matrix_setup_configuration #{setup}.split(//) # global setup
    matrix_setup_#{action} #{setup}, #{expected} # action setup + execution
    matrix_test_#{expected}, #{setup} # expected verification
end
So at the end the setup configuration will be factored at one place taking into parameters the different edge cases, the actions and assertions will also be put apart.

Ryan Davis gives a clear visual example of test cases before and after matrixization:

Before After

Click the images to see the code in more detail.


The test matrix model is another demonstration of the DRY (Don't Repeat Yourself) process philosophy. It also easily makes it possible for non-developer to read and add tests by only modifying the matrix.

No comments

Watch Thread Reply

Educational Content

Questions for an Enterprise Architect

Erik Dörnenburg answers: What is Enterprise and Evolutionary Architecture?, discussing 4 issues: Turning strategy into execution, Ensuring conformance, Where do the architects sit? Buying or building?

Wrap Your SQL Head Around Riak MapReduce

Sean Cribbs explains what Map-Reduce and Riak are, why and how to use Map-Reduce with Riak, and how to convert SQL queries into their Map-Reduce equivalents.

Polyglot Persistence for Java Developers - Moving Out of the Relational Comfort Zone

Chris Richardson shows how he ported a relational database to three NoSQL data stores: Redis, Cassandra and MongoDB.

The Golden Circle – Why How What

Jean Tabaka challenges the audience to reflect on what Agile practices they are employing, how they are using them, ending with the questions “Why have their organization chosen to go Agile?

The Web Platform as a Limitless Pool of Innovation, with Andreas Gal

Andreas talks about the benefits of the Open Web and how it compares to proprietary stacks. He also talks about various projects that push the envelope like Boot to Gecko, Broadway and pdf.js.

Hadoop and NoSQLin a Big Data Environment

Ron Bodkin discusses early adoption of Hadoop, NoSQL and describes MapReduce and related libraries and Frameworks. Other topics include Hive, Pig, multi tenancy, and security in a big data environment

Spring and Platform Interoperability

Stephen Bohlen explains how Spring helps with interoperability between Java and .NET, demoing it with the help of a sample application.

How to Stop Writing Next Year's Unsustainable Piece of Code

Guilherme Silveira mentions some of the turning points in project development that may affect the quality of the code offering advice on avoiding writing crappy code.