Test data specification to generate actual test data
I don't want to describe the domain I work in at my work, so I will describe some fake scenario; might be silly but hopefully illustrative enough. Let's say we have a system that qualifies whether an employee deserves for promotion or not and want to test it.
The system recognizes such candidates for promotion by counting how many hours in a row a user did overtime, how many commitments it completed. It does not operates on statistics, but do calculate them themselves based on the work registered by the user.
I could set data for test in a very precise way:
createUser() .with(overtime().on(DayOfWeek.FRIDAY).from(16).to(20)) .with(overtime().on(DayOfWeek.SATURDAY).from(10).to(15)) .with(overtime().on(DayOfWeek.SUNDAY).from(14).to(19)) .with(commitment().plannedFor(days(5)).completedIn(days(1)) .with(commitment().plannedFor(days(1)).completedIn(days(3));
But I could also define a specification for the test data that is more concise
createUser() .with(overtime().of(hours(13).including(weekends())) .with(commitments().completedBeforeDeadline(0.5))
and is used by the test framework to generate actual data for the tests.
Having this kind of abstraction in the test (only a few most important variables), I am immediately able to grasp the idea of test. Also, I can write much more combinations of test in a short time.
I know we haven't invented a wheel, so I would like to read more about this approach, its risks etc but don't know how to search for it. How do you call it? Is it somehow related to BDD? Or what?
That is the Builder pattern.
It is not specific to BDD. It's fairly common in integration tests and functional tests. It's less used in unit test, which typically have less need for complex data setup.
A test data builder might either construct data, or find relevant data in a database. (I strongly prefer building data from scratch, but that's a rant for another day.)
Your second example focuses very nicely on the intention of the data.
If the essence of the test is that the overtime be on weekends, the first example clutters the test with incidental details that do not clearly express the intention. A reader could maybe puzzle it out, especially if the test name indicates that the test has something to do with weekend overtime.