Testing puzzles

Discussion of all aspects of the game engine, including development of new and existing features.

Moderator: Forum Moderators

Post Reply
CrawlCycle
Posts: 27
Joined: November 20th, 2020, 5:07 am

Testing puzzles

Post by CrawlCycle »

Create a new file for unit tests
  • Create a new file within wesnoth/src/test
  • Add the path of the new file to the list at wesnoth/source_lists/boost_unit_tests
I made a typo in wesnoth/source_lists/boost_unit_tests, but CMake didn't warn.

Parametrized test

Parametrized test has two advantages:
  • It allows running the same test for a set of parameters.
  • It reports the values of the parameters upon failure of the test.
Example for Boost 1.59+

Code: Select all

#include <boost/test/unit_test.hpp>
// Include test_case.hpp after unit_tests.hpp to work around
// https://svn.boost.org/trac10/ticket/13387
#include <boost/test/data/test_case.hpp>  // for parametrized test

namespace bdata = boost::unit_test::data;
BOOST_AUTO_TEST_SUITE ( test_suit_name )
BOOST_DATA_TEST_CASE (
    	test_name,
    	bdata::make({true, false}) * bdata::make({"A", "B", "C"}),
    	my_bool, my_key )  // 2 * 3 = 6 cases
{
	BOOST_CHECK_EQUAL (1, 0);
}
BOOST_AUTO_TEST_SUITE_END()
Error:

Code: Select all

error: in "test_suit_name/test_name/_0": check 1 == 0 has failed [1 != 0]
Failure occurred in a following context:
    my_bool = true; my_key = A;
error: in "test_suit_name/test_name/_1": check 1 == 0 has failed [1 != 0]
Failure occurred in a following context:
    my_bool = true; my_key = B;
Installation guide says Building Wesnoth only requires Boost >= 1.56.
While macro BOOST_PARAM_TEST_CASE of Boost <= 1.58 enables parametrized, the macro has two problems:
  • It doesn't automatically register the test.
  • It can't work with more than one parameter.
New macro BOOST_DATA_TEST_CASE of Boost >= 1.59 is much better.
Per discussion at the pull request, using the new macro is fine.

src/tests/utils has a auto_parameterized.hpp

Naming Unit Test
  • Format: method_name_StateOfThis_allArguments_AllExpectedBehaviors
  • Character limit: 100 characters
Example 1

Code: Select all

BOOST_AUTO_TEST_CASE ( add_child_EmptyThis_simpleKey_AppendAndReturnNewEmptyChild )
{
	config actual;
	const config new_child = actual.add_child("A");
	const config expected("A");
	// Assert add_child adds the child correctly
	BOOST_CHECK_EQUAL (actual, expected);
	// Assert the new child returned by config::add_child is correct
	BOOST_CHECK_EQUAL (new_child, config());
}
Last edited by CrawlCycle on November 25th, 2020, 7:51 pm, edited 8 times in total.
CrawlCycle
Posts: 27
Joined: November 20th, 2020, 5:07 am

Re: Testing puzzles

Post by CrawlCycle »

How to name a unit test I am thinking about method_name_StateOfThis_allArguments_AllExpectedBehaviors.

Cast study 1
  • Name of the method is: add_child
  • State of this object before calling the method: Empty
  • Argument provided to the method: simple key
  • Expected behaviour of the method:
    • The method appends an empty child to this object.
    • The method returns the new child
Name of the test is:
add_child_EmptyThis_simpleKey_AppendAndReturnNewEmptyChild
test of add_child method of config

Code: Select all

BOOST_AUTO_TEST_CASE (
		add_child_EmptyThis_simpleKey_AppendAndReturnNewEmptyChild )
{
	config actual;
	const config new_child = actual.add_child("A");
	const config expected("A");
	// Assert add_child adds the child correctly
	BOOST_CHECK_EQUAL (actual, expected);
	// Assert the new child returned by config::add_child is correct
	BOOST_CHECK_EQUAL (new_child, config());
}
Last edited by CrawlCycle on November 25th, 2020, 7:50 pm, edited 2 times in total.
CrawlCycle
Posts: 27
Joined: November 20th, 2020, 5:07 am

Re: Testing puzzles

Post by CrawlCycle »

I don't like long unreadable names. But why do people still use that?
Until I know all of the reason, I probably would still use long names.

Alternatively, maybe I can write a custom struct that replaces a long name:
  • Fields for holding the properties of the test
  • An implicit conversion to string.
User avatar
Celtic_Minstrel
Developer
Posts: 2166
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Testing puzzles

Post by Celtic_Minstrel »

I don't think the string conversion idea will work - you can't make it convert to an identifier, after all.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
Post Reply