I'm writing unit tests for one of Translate classes.
In the setUp I need to create few pages, but I need to also control the user ids of the revisions. This seems to work well except for two things: * dataProvider methods are called *before* setUp, so I cannot use the user ids I have stored in setUp. * setUp and tearDown are called for *every* item in the dataProvider. This seems very wasteful - no wonder the tests takes minutes or so to run.
This just doesn't make any sense to me. I'm considering to stop using @dataProvider in this case - any other ideas?
The code in setUp is something like this:
$title = Title::makeTitle( NS_MEDIAWIKI, 'Key1/fi' ); $user = User::newFromName( 'Translate test user 1' ); $user->addToDatabase(); WikiPage::factory( $title )->doEdit( 'trans1', __METHOD__, 0, false, $user ); $this->user1 = $user;
-Niklas
This is a functionality of PHPUnit, not of MediaWiki. The idea is that each test case (or function) is considered the most basic unit of testing. So setUp() and tearDown() have to be called before and after each function to ensure the testing environment is clean for the test.
*--* *Tyler Romeo* Stevens Institute of Technology, Class of 2015 Major in Computer Science www.whizkidztech.com | tylerromeo@gmail.com
On Mon, Sep 24, 2012 at 12:18 AM, Niklas Laxström <niklas.laxstrom@gmail.com
wrote:
I'm writing unit tests for one of Translate classes.
In the setUp I need to create few pages, but I need to also control the user ids of the revisions. This seems to work well except for two things:
- dataProvider methods are called *before* setUp, so I cannot use the
user ids I have stored in setUp.
- setUp and tearDown are called for *every* item in the dataProvider.
This seems very wasteful - no wonder the tests takes minutes or so to run.
This just doesn't make any sense to me. I'm considering to stop using @dataProvider in this case - any other ideas?
The code in setUp is something like this:
$title = Title::makeTitle( NS_MEDIAWIKI, 'Key1/fi' ); $user = User::newFromName( 'Translate test user 1' ); $user->addToDatabase(); WikiPage::factory( $title )->doEdit( 'trans1', __METHOD__,
0, false, $user ); $this->user1 = $user;
-Niklas
-- Niklas Laxström
Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
Le 24/09/12 06:18, Niklas Laxström a écrit :
I'm writing unit tests for one of Translate classes.
In the setUp I need to create few pages, but I need to also control the user ids of the revisions. This seems to work well except for two things:
- dataProvider methods are called *before* setUp, so I cannot use the
user ids I have stored in setUp.
- setUp and tearDown are called for *every* item in the dataProvider.
This seems very wasteful - no wonder the tests takes minutes or so to run.
A data provider give a stream of cases that will be used as an independent test. Kind of save you from copy pasting the same test function over and over.
setUp() is a method called before each test, so it is indeed called before each of the cases provided via a data provider. Similarly, tearDown() is called after each test.
This just doesn't make any sense to me. I'm considering to stop using @dataProvider in this case - any other ideas?
The code in setUp is something like this:
$title = Title::makeTitle( NS_MEDIAWIKI, 'Key1/fi' ); $user = User::newFromName( 'Translate test user 1' ); $user->addToDatabase(); WikiPage::factory( $title )->doEdit( 'trans1', __METHOD__, 0, false, $user ); $this->user1 = $user;
One possibility would be to only set it once using the setUpBeforeClass() and tearDownAfterClass(). Those are only run once in the class. So you could create a new test file having a class dedicated to this test.
You could probably skip the addToDatabase() call if the user is already in there. Note your code will produce an edit for any case.
Can you possibly send the code in Gerrit so we can have a look at it?
Antoine Musso wrote:
One possibility would be to only set it once using the setUpBeforeClass() and tearDownAfterClass(). Those are only run once in the class. So you could create a new test file having a class dedicated to this test.
Oh, I didn't know those existed...
Niklas, implement addDBData() method for that (called by MediaWikiTestCase)
On 24 September 2012 13:27, Antoine Musso hashar+wmf@free.fr wrote:
One possibility would be to only set it once using the setUpBeforeClass() and tearDownAfterClass(). Those are only run once in the class. So you could create a new test file having a class dedicated to this test.
Those are static methods, I cannot store the users anywhere if I use those.
Can you possibly send the code in Gerrit so we can have a look at it?
Yes.
-Niklas
wikitech-l@lists.wikimedia.org