-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Means for writing test cases more concisely #2587
Comments
I do not see what is the state you need to maintain between the test cases given that you are running the same test case multiple times but with different input/output. You mention that Sections can work for you, but for the fact that you cannot run only one of the section at a time. In that situation, It might be possible to factor out the setup/test code into a function/class. And then have each test case use that function/class to run the test case for that particular set of input and output files.
|
Due to the fact that the inputs are quite short, I initially thought about keeping all the inputs in the same file, hence the need for maintaining state. Having separate input files would certainly be an easy solution. |
So you want to have a file like inputs:
- name: foo
key1: value1
key2: value2
- name: bar
key1: value1
key2: value2
... and then a test case that goes over each input in a row? With the shared state being your position in the input file? That is exactly what the generators are for. (or just a for loop on top of the test case, advantage of using generators is better reusability and integration with the code) |
Thanks for the pointer! I’ll look into using generators this way. |
Description
I have a number of scenarios that run basically the same test with different inputs and expected outputs. I would like to write the tests as concisely as possible, i.e. minimise the amount of boilerplate per test. This can be achieved by e.g. using a custom file format for the inputs and outputs and describing the tests in such a file, but this leads to somewhat convoluted implementation, since currently there is no way to maintain state between scenarios or test cases (other than using global variables). Additionally, some sets of inputs need common setup, the results of which I thought would be nice to reuse, since the tests are not meant to evaluate that part of the code. I use the 2.x branch, so there may be some new functionality for this purpose of which I am not aware.
I am not sure what would be the best solution to address this or if it is even worthwhile. A straightforward approach would be to have test suites (as described in an earlier ticket) that would be able to maintain state between test cases or scenarios. Another approach would be describing the scenarios and the required steps separately like Cucumber does. The former solution could have also some other uses cases but would require the library user to implement reading the input, while the latter would presumably require more changes in Catch2.
Additional context
So far I have tried two different options.
WHEN
andTHEN
are placed outside the scenario, which makes the code slightly more difficult to follow.The text was updated successfully, but these errors were encountered: