Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test are too general #43

Open
Hordex opened this issue Jun 7, 2017 · 0 comments
Open

Unit test are too general #43

Hordex opened this issue Jun 7, 2017 · 0 comments

Comments

@Hordex
Copy link

Hordex commented Jun 7, 2017

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
Projects
None yet
Development

No branches or pull requests

2 participants