Changes between Initial Version and Version 1 of WritingTests


Ignore:
Timestamp:
Aug 7, 2009, 5:02:10 PM (15 years ago)
Author:
Philip Taylor
Comment:

basic test information

Legend:

Unmodified
Added
Removed
Modified
  • WritingTests

    v1 v1  
     1We use the [http://cxxtest.tigris.org/ CxxTest] framework for tests. See the [http://cxxtest.com/guide.html CxxTest user guide] for an introduction and general reference.
     2
     3Tests are highly recommended for new code, and for bug fixes to old code, because they provide some assurances of quality and help detect regressions and portability issues. Sometimes code might need to be redesigned a bit so that it's more easily testable, which is fine.
     4
     5There are a few issues specific to our game:
     6
     7== Test locations ==
     8
     9The tests for file `foo/bar/Baz.{cpp,h}` usually go into `foo/bar/tests/test_Baz.h`, and have the following form:
     10{{{
     11/* Copyright (C) 2009 Wildfire Games.
     12 * This file is part of 0 A.D.
     13 *
     14 * 0 A.D. is free software: you can redistribute it and/or modify
     15 * it under the terms of the GNU General Public License as published by
     16 * the Free Software Foundation, either version 2 of the License, or
     17 * (at your option) any later version.
     18 *
     19 * 0 A.D. is distributed in the hope that it will be useful,
     20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     22 * GNU General Public License for more details.
     23 *
     24 * You should have received a copy of the GNU General Public License
     25 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
     26 */
     27
     28#include "lib/self_test.h"
     29
     30#include "foo/bar/Baz.h"
     31
     32class TestBaz : public CxxTest::TestSuite
     33{
     34public:
     35    void test_whatever()
     36    {
     37        ...
     38    }
     39};
     40}}}
     41
     42== New assertion macros ==
     43
     44`TS_ASSERT_OK(expr)` -- assert `expr == INFO::OK`.
     45
     46`TS_ASSERT_STR_EQUALS(str1, str2)` -- assert `std::string(str1) == std::string(str2)`.
     47
     48`TS_ASSERT_WSTR_EQUALS(str1, str2)` -- assert `std::wstring(str1) == std::wstring(str2)`.
     49
     50`TS_ASSERT_STR_CONTAINS(str1, str2)` -- assert `str2` is contained somewhere within `str1`.
     51
     52`TS_ASSERT_VECTOR_EQUALS_ARRAY(vec1, array)` -- assert a `std::vector` is equal to a constant-sized array.
     53
     54== Mocks ==
     55
     56Particularly for testing code that relies on global system functions (`time`, `getcwd`, etc), it can be useful to replace the functions with mock objects that return special values or do special checks on their arguments. The !CxxTest guide describes how to do that. We use the following specifics:
     57 * To mock functions from `foo.h`, create the file `mocks/foo.h` saying:
     58{{{
     59#include <foo.h>
     60#include <cxxtest/Mock.h>
     61CXXTEST_MOCK_GLOBAL(...)
     62CXXTEST_MOCK_GLOBAL(... etc, as in the CxxTest guide ...)
     63}}}
     64 * Update `mocks/mocks_real.cpp` to add the line `#include "mocks/foo.h"`
     65 * Update `mocks/mocks_test.cpp` to add the line `#include "mocks/foo.h"` and, if you want to default to calling the real function (instead of an empty dummy function) in tests that don't override it then add `DEFAULT(foo);`
     66 * In any source file that should use the mocks, do a `#include "mocks/foo.h"` (instead of the original `#include <foo.h>`), and use `T::func()` instead of `func()`.
     67
     68== Running tests ==
     69
     70To run a single test suite instead of all tests, add the command-line argument `-test TestSuitename`. To run a single test, do `-test TestSuitename::test_case_name` (using the class/method names from the test code).