catchorg / catchorg/Catch2

Teardown Clarification and/or Feature Request

Open
#1,078 10 comments 2 reactions 0 assignees View on GitHub
Query
Dominant language
C++
Stars
21.5k
Forks
3.5k
Avg merge
3d 16h
Merged PRs (30d)
2

Description

## Description
First, this is an awesome testing framework. It behaves similarly to other testing frameworks that I've used to higher level languages and is still simple and intuitive, which is pretty impressive.

In the documentation, teardown is mentioned several times, but a clear example is never provided. I would assume that code written after a test case (`SECTION`) would behave as teardown; however, [this issue](https://github.com/catchorg/Catch2/issues/851) appears to disagree with that intuition. That same issue mentions `Listener`s, which isn't really mentioned in the main documentation anywhere when setup/teardown is mentioned; however, that doesn't quite fit the bill either. Unlike setup, there doesn't really appear to be a nice, concise way to write teardown logic for a specific `SECTION` of tests. Rather it defines a way to listen for _all_ tests ending. This would be great for global setup/teardown, but I'm not sure it would quite work for specific tests.

I also found fixtures, which seems promising, but its not clearly documented when the constructor/destructor are called. If they are called before/after each test, that would work great.

Here is a simple example of what I am looking for:
```c++
TEST_CASE("Database", "[database]") {
SECTION("Database::open()") { /*...*/ }
SECTION("Database.close()") { /*...*/ }

SECTION("Database.query()") {
// Set-up this set of tests by opening a connection to the Database and populating it.
Database database = Database("connectionString", "flags");

database.createTable(/* blah blah */);
database.insert(/* blah blah */);

/* Test querying the database */

// Tear-down after **each** test by clearing all data from the DB and closing the connection to it. I don't want to affect future tests!
database.dropTable(/* blah blah */);
database.close();
}
}
```

In the example above, I don't want to create a connection to the database when testing `open()`; hence, I only define that logic in the `Database.query()` `SECTION`. Similarly, I don't want to close the connection after each test when testing `close()`. However, this is pretty important when testing the query functionality.

So, is there a way to accomplish this as is? If so, this issue is a request for documentation on the subject that is either discussed or directly linked to in the main documentation. If not, this issue is a feature request.

### Extra information
* Catch version: **v2.0.1**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.