Skip to content

Unit test are too general #43

Open

Description

@Hordex

Single unit test cover too much functionality, example from DynarrayTests.cpp:

TEST_CASE("Dynarray constructors", "[Dynarray]")
{
	// default constructor
	Dynarray<int> a;
	REQUIRE(a.GetSize() == 0);
	
	// initializer list constructor
	Dynarray<int> b{ 1, 2, 3 };
	REQUIRE(b.GetSize() == 3);
	REQUIRE(b[0] == 1);
	REQUIRE(b[1] == 2);
	REQUIRE(b[2] == 3);

	// reserve constructor
	Dynarray<int> c(5);
	REQUIRE(c.GetSize() == 0);
	REQUIRE(c.GetCapacity() >= 5);

	// copy constructor
	Dynarray<int> d(b);
	REQUIRE(b.GetSize() == 3);
	REQUIRE(d.GetSize() == 3);
	REQUIRE(d[0] == 1);
	REQUIRE(d[1] == 2);
	REQUIRE(d[2] == 3);

	// move constructor
	Dynarray<int> e(std::move(b));
	REQUIRE(b.GetSize() == 0);
	REQUIRE(e.GetSize() == 3);
	REQUIRE(e[0] == 1);
	REQUIRE(e[1] == 2);
	REQUIRE(e[2] == 3);
}

should be like:

TEST_CASE("Dynarray default constructor", "[Dynarray]")
{
	Dynarray<int> a;
	REQUIRE(a.GetSize() == 0);
}

TEST_CASE("Dynarray initializer list constructor", "[Dynarray]")
{
	Dynarray<int> b{ 1, 2, 3 };
	REQUIRE(b.GetSize() == 3);
	REQUIRE(b[0] == 1);
	REQUIRE(b[1] == 2);
	REQUIRE(b[2] == 3);
}

TEST_CASE("Dynarray reserve constructor", "[Dynarray]")
{
	Dynarray<int> c(5);
	REQUIRE(c.GetSize() == 0);
	REQUIRE(c.GetCapacity() >= 5);
}

TEST_CASE("Dynarray copy constructor", "[Dynarray]")
{
	Dynarray<int> b{ 1, 2, 3 };
	Dynarray<int> d(b);
	REQUIRE(b.GetSize() == 3);
	REQUIRE(d.GetSize() == 3);
	REQUIRE(d[0] == 1);
	REQUIRE(d[1] == 2);
	REQUIRE(d[2] == 3);
}

TEST_CASE("Dynarray move constructor", "[Dynarray]")
{
	Dynarray<int> b{ 1, 2, 3 };
	Dynarray<int> e(std::move(b));
	REQUIRE(b.GetSize() == 0);
	REQUIRE(e.GetSize() == 3);
	REQUIRE(e[0] == 1);
	REQUIRE(e[1] == 2);
	REQUIRE(e[2] == 3);
}

This makes adding new tests more intuitive and reading reports easier and informative. After all failing test "Dynarray copy constructor, at line XYZ" says more than failing test "Dynarray constructors, at line ZYX"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions