Testing

evantahler edited this page Apr 10, 2011 · 3 revisions

An Action testing framework is PART of the DAVE project, and is located in /SPEC. This framework is meant to help you test the responses of this API. This is not a unit testing framework for individual PHP Methods, as many of these exist already and can be integrated with this framework. This test suite is meant to ensure that the end-to-end behavior works as expected.

This test suite can be deployed against your production or local environments. The need to test fast locally was the primary motivation for developing SERVER, and is great for this purpose.

Creating a Test with spec_helper.php

The easiest way to create a test is to use to include spec_helper.php and make use of the DaveTest object. This object makes use of the APIRequest class (API/AccessTools/APIRequest.php) to make true http requests to your server. In this way you can assert and check various functionality of your API.

Simple test example

require_once("../spec_helper.php");
$T = new DaveTest("General API Tests");

$T->context("I should have an IP address reported by the server");
    $PostArray = array("OutputType" => "PHP");
    $APIRequest = new APIRequest($TestURL, $PostArray);
    $APIDATA = $APIRequest->DoRequest();
    $T->assert(">",strlen($APIDATA["IP"]),0);

$T->end();

In this test, I am asserting that the server returns to me my IP address, and its string length is greater than 0.
The test suite needs to be initialized with a new DaveTest object. This object requires a description of the test suit it is to contain (for logging). The file can be executed directly by PHP CLI.

Various contexts (and sub-contexts) can be used to help explain your test suite and reporting will be clearer if you do so. You can provide an optional parameter to context("context_name", depth), where depth is an integer which describes a hierarchy to your tests, again for reporting purposes.

Test output is sent both to the terminal, and to the $CONFIG['TestLog'] file. Please note that there are special configuration options in API/CONFIG.php for the test suite:

$CONFIG['ServerAddressForTests'] = "127.0.0.1:3000";
$CONFIG['TestRootFolder'] = $CONFIG['App_dir']."../SPEC/";
$CONFIG['TestLog'] = $CONFIG['App_dir']."../SPEC/LOG/test_log.txt";

DaveTest Object

The DaveTest object has the following methods:

  • assert($evaluation_method, $a, $b) : see below
  • end() : used to close and log the end of a testing session
  • log() : to log an arbitrary string to the test log and terminal. Requires a string as input.
  • context() : to denote a testing context within a test suite (DaveTest object). Requires a string name as input.

Assertions

assert($evaluation_method, $a, $b) The DaveTest object contains the Assert() method which can be used to test one thing against another. Assertions are logged to the test output. Assert() takes a string of the evaluation_method, and then the (up to) 2 things to compare. The evaluation methods at this time are:

  • "=="
  • "!="
  • "<"
  • ">"
  • "<="
  • ">="
  • "empty"
  • "null"
  • "true"
  • "false"
  • "in_array"
  • "not_in_array"

RollBack

Testing requests can also send the Rollback parameter. Sending this parameter will cause DAVE to respond as he would normally, but for the information not to be committed to the database. This is useful, for example, if you want to test the UserAdd action. Post all the proper parameters and DAVE will respond successfully and even assign you a new userID. However, this user will never make it into the DB. The syntax is &Rollback=RollbackPhrase. $CONFIG['RollbackPhrase'] is defined in CONFIG.php

  • Your database type for mySQL must support transactions (like InnoDB)
  • Requests will still be logged normally
  • Auto-Incremented columns are still incremented by rolled-back transactions

TEST.php

To run all tests within the /SPEC folder (or other folder defined by $CONFIG['TestRootFolder']), simply run TEST.php from the command line.

Note

At this time, please only create single folders from with in the /SPEC folder for your tests. Deeper folders cause errors.