-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Fixture Level Data Provider #3212
Comments
Please explain the problem you are trying to solve first before you propose an implementation. |
@sebastianbergmann I didn't propose implementation, just showed how I want to be able to write some tests suites. The problem I am trying to solve is to set up multiple environments for the same test suite. Suppose a fixture that have 20+ tests (or 200+ or 2000+ for that matter). And I want to test it on multiple platforms (say I'm testing a website using selenium on saucelabs or browserstack, and want to test it on multiple devices, in multiple resolutions, multiple browsers, etc.). Is it possible to do now or is this a missing feature? |
You are (probably) mixing up test case (a class that extends (directly or indirectly)
Just to clairfy: the example code you showed in #3212 (comment) is what I meant by "proposing an implementation" in #3212 (comment). This does not help me understand what you want. While trying to understand what it is you are requesting I came up with two possibilities of what it might be you want:
|
Thank you for the clarification. |
I think I have a similar use case: I'm trying to test a database abstraction layer, that sits on top of mysqli/PDO/etc:
My tests look like:
The problem lies with the PDOConnectionTest: I want to run all the tests inside this class, for many configurations:
For MySQL/SQLite, it makes sense to subclass: I will have some setup code in each subclass, that returns a PDO connection to the relevant database. For the PDO attributes however, it would be really cumbersome to create a subclass for each combination:
As you can see, this gets very ugly very quick. I haven't found a way so far, to run a full test class several times, with different parameters. Some kind of class-level data provider, indeed. Did I miss something? |
@sebastianbergmann Any advice/feedback? |
I had not seen this issue previously. Linking it to the |
Well, if you're willing to step down from the There is a library for PhpUnit, called interface Connection{
function beforeEach(); // or whatever methods you'd like
function afterEach();
}
// implementations of different databases
class MySqlConnection{}
class PdoMysqlConnection{}
class PdoPostgresConnection {} And your test might look something like this class MyTest extends TestCase {
/**
* @test
* @dataProvider provider
*/
public function test(Connection $con, bool $arg1, string $arg2, string $type) {
// given
$con->beforeEach();
// when
// some testing
// then
$con->afterEach();
}
public function provider(): array {
return DataProviders::cross(
$this->connections(),
$this->args(),
$this->types(),
);
}
public function args(): array {
return [
[true, 'fish'],
[true, 'dog'],
[false, '']
];
}
public function types(): array {
return [
['local'],
['remote']
];
}
public function connections(): array {
return [
[new MysqlConnection()],
[new PdoMysqlConnection()],
[new PdoPostgresConnection()]
];
}
} |
I support the issue, but I think the title is misleading. And yes,
This is spot on. My own use case: The main test*() method is defined in a base class, and from there we call various protected methods that can be overridden in subclasses. (I would normally make these abstract, but in this case it makes sense to have default implementations.) Some of the child test classes cover settings that have a range of possible values. This range could be mapped with a data provider, but here comes the problem: The data provider would be specific to the child class, but the test method sits in the base class, and does not have a parameter signature. Also, we already want to use some of these values in the setUp() method, before the test*() method is called. |
@sebastianbergmann I see you closed this as completed... what was the resolution? Were any changes made? |
I'm missing a feature common in other unit testing frameworks of data provider for the entire test fixture.
I want to be able to write something like this:
This example is of course a very simplified scenario based on the example in PHPUnit docs, and what this feature is usually used for is to setup various environments, and running all the tests in the fixture on each environment.
Did I miss this feature (or something else that will allow me to set up various environments) in the docs? If I didn't I think it would be a nice feature to add. I know it will definitely help me a lot.
The text was updated successfully, but these errors were encountered: