Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7980635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:08:06+00:00 2026-06-04T10:08:06+00:00

I have a testclass in which one test runs multiple times via a @dataProvider

  • 0

I have a testclass in which one test runs multiple times via a @dataProvider and another test that @depends on the first method. However, when I called var_dump on what should be passed to the second test, it gives me a solid NULL, which I didn’t expect.

In other words: what should this do:

<?php 
class DevicesTest extends PHPUnit_Framework_TestCase
{
    /**
     * @dataProvider registerDataProvider
     */
    public function testRegister($device, $supposedResult)
    {
        //do a bunch of tests
        return array($device, $supposedResult);
    }

    public function registerDataProvider()
    {
        return array(array("foo", "foo"));
    }

    /**
     * @depends testRegister
     */
    public function testSaveDevicePreferences($deviceArr)
    {
        $this->assertNotEmpty($deviceArr);
    }
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:08:08+00:00Added an answer on June 4, 2026 at 10:08 am

    Normally @dataProvider is used when you want to run a test multiple times with different data sets for each. It exists to save you from writing looping code in the test and to allow different data sets to pass or fail individually.

    As I said in my comments, I believe that PHPUnit will use either @depends or @dataProvider, and from your example my guess is that the second wins out. Another possibility is that tests with data providers cannot be used as dependencies because PHPUnit doesn’t know which test-plus-dataset to pick.

    Since registerDataProvider returns a single data set, you could just as easily call it from the test itself. This would allow @depends to work in the second test without the @dataProvider. Assuming that testRegister needs to modify $device and/or $supposedResult, this should work:

    class DevicesTest extends PHPUnit_Framework_TestCase {
        public function testRegister() {
            list($device, $supposedResult) = $this->registerDataProvider();
            //do a bunch of tests
            //register a device in the DB
            return array($device, $supposedResult);
        }
    
        public function registerDataProvider() {
            return array("foo", "foo");
        }
    
        /**
         * @depends testRegister
         */
        public function testSaveDevicePreferences($data) {
            list($device, $supposedResult) = $data;
            // do stuff dependent on testRegister()
            $this->assertEquals($device, $supposedResult);
        }
    }
    

    If those variables don’t need to be modified by the first test, you can simply call registerDataProvider from both tests. Note that PHPUnit will not separate a returned array from a dependend upon test into arguments to the dependent test as the data provider mechanism does. This is because it doesn’t know that the array being returned is multiple arguments versus a single argument.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I have more than one method with @Parameters in junit test class which
I have (Test-A) fully annotated testng test in one project. Note: this test runs
I have a component (link to Test class) which contain one line in it,
I have a JUnit test class in which I have several static final int
I have this inside my spring xml file: <object type=Test.Web.Utilities.TestClass, Test.Web singleton=false id=TestClass> <property
I have the following method for which I am trying to write a unit
So i have a small integration test that houses 5 tests in total. Running
I have a class test which isn't standard constructable nor assignable due to certain
I have a JUnit test case in an Android project that contains code that
I have an ant task set up like so: <target name=unit-test description=unit tests depends=compile-tests>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.