Create a SCRIMMAGE Test

Unit testing is often added to a project to ensure that new code works and doesn’t break existing functionality. A common library for adding tests is googletest where one can test individual functions or classes. However, it is often only when all of these functions and classes are combined together in a simulation that edge cases are found. Thus, SCRIMMAGE provides a high level testing framework to test mission level functionality. A sample can be found in scrimmage/test/test_collisions.cpp:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <gtest/gtest.h>

#include <scrimmage/common/CSV.h>
#include <scrimmage/simcontrol/SimUtils.h>

#include <boost/optional.hpp>

namespace sc = scrimmage;

TEST(test_angles, rotation) {
    const std::string mission = "straight";
    auto log_dir = sc::run_test(mission);

    bool success = log_dir ? true : false;
    EXPECT_TRUE(success);
    if (!log_dir) return;

    sc::CSV csv;
    bool summary_found = csv.read_csv(*log_dir + "/summary.csv");
    EXPECT_TRUE(summary_found);
    if (!summary_found) return;

    const int row = csv.rows() - 1;
    double collisions = csv.at(row, "team_coll");
    EXPECT_GT(collisions, 0); // expect collisions
}

Here are links to gtest, scrimmage CSV File Logging, and boost optional. We now walk through what including SimUtils.h does. The following line specifies what mission file to run (it uses find_mission to find the file at scrimmage/missions/straight.xml) and then runs the mission. The output is a boost::optional that can be dereferenced to the output log directory if the mission was successful:

const std::string mission = "straight";
auto log_dir = sc::run_test(mission);
bool success = log_dir ? true : false;
EXPECT_TRUE(success);
if (!log_dir) return;

We now need to verify that the simulation passed our test. Here we are checking that there was at least one collision in the simulation by reading the summary.csv that is output by a metrics plugin (see Create a Metrics Plugin):

sc::CSV csv;
bool summary_found = csv.read_csv(*log_dir + "/summary.csv");
EXPECT_TRUE(summary_found);
if (!summary_found) return;

const int row = csv.rows() - 1;
double collisions = csv.at(row, "team_coll");
EXPECT_GT(collisions, 0); // expect collisions