Skip to content
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

Unittest parameterization #267

Closed
thekid opened this issue Mar 30, 2013 · 3 comments
Closed

Unittest parameterization #267

thekid opened this issue Mar 30, 2013 · 3 comments

Comments

@thekid
Copy link
Member

thekid commented Mar 30, 2013

Scope of Change

This will extend the unittest API by adding a facility for providing values to a test.

Rationale

This will make it easier to discover problems in tests written using foreach loops currently, and at the same time reduce the amount of code that needs to be written for tests with separate test methods for every value.

Functionality

Parameterized test case methods can be annotated with a @values annotation.

Value sources

Inline

The values are taken from the annotation itself, as an array:

#[@test, @values(array(1, 2, 3))]
public function fixture($value) {
}

Locally sourced

The values are returned from a public instance method inside the current class

public function values() {
  return array(1, 2, 3);
}

#[@test, @values('values')]
public function fixture($value) {
}

Outsourced

The values are returned from a public static method inside a class specified by a double-colon notation.

class ValueProvider extends Object {
  public static function values() {
    return array(1, 2, 3);
  }
}

#[@test, @values('ValueProvider::values')]
public function fixture($value) {
}

Method sources - arguments

For the values sourced to methods, additional arguments may be provided by using the named annotation form. The source method is then invoked with the given arguments. If the args key is omitted, the method is invoked without any arguments (and may thus use their defaults):

public function range($lo= 1, $hi= 3) {
  return range($lo, $hi);
}

#[@test, @values(source= 'range', args= array(1, 10))]
public function fixture($value) {
}

Example 1: Refactoring multiple tests

This example collapses two separate methods into using the inline value source.

Before

  /**
   * Test name[=...
   *
   */
  #[@test, @expect('lang.FormatException')]
  public function unbalanced_opening_bracket_in_key() {
    $this->fixture->deserialize($this->input('name[=...'), Type::$VAR);
  }

  /**
   * Test name]=...
   *
   */
  #[@test, @expect('lang.FormatException')]
  public function unbalanced_closing_bracket_in_key() {
    $this->fixture->deserialize($this->input('name]=...'), Type::$VAR);
  }

After

  /**
   * Test unbalanced brackets such as "name[=..." and "name]=..." raise exceptions.
   *
   */
  #[@test, @expect('lang.FormatException'), @values(array('name[=...', 'name]=...'))]
  public function unbalanced_bracket_in_key($key) {
    $this->fixture->deserialize($this->input($key), Type::$VAR);
  }

Security considerations

n/a

Speed impact

Negligible

Dependencies

n/a

Related documents

@thekid
Copy link
Member Author

thekid commented Apr 9, 2013

Announced in the blog - http://news.planet-xp.net/article/510/2013/04/01/

@thekid
Copy link
Member Author

thekid commented Apr 9, 2013

A good example: https://github.com/xp-forge/mustache/blob/master/src/test/php/com/github/mustache/unittest/SpecificationTest.class.php

$ unittest com.github.mustache.unittest.SpecificationTest -a spec-master/specs
[........................................................................
..................................................]

OK: 122/122 run (0 skipped), 122 succeeded, 0 failed
Memory used: 2748.67 kB (2965.49 kB peak)
Time taken: 0.034 seconds

thekid added a commit to xp-framework/xp-framework that referenced this issue May 5, 2013
@thekid thekid closed this as completed May 5, 2013
@thekid
Copy link
Member Author

thekid commented May 13, 2013

What a massive ease this can be can be seen by looking at xp-framework/xp-framework@c492823

thekid added a commit to xp-framework/remote that referenced this issue Nov 10, 2013
# See also pull request #273
thekid added a commit to xp-framework/doclet that referenced this issue Nov 10, 2013
# See also pull request #273
thekid added a commit to xp-framework/zip that referenced this issue Nov 10, 2013
# See also pull request #273
thekid added a commit to xp-framework/rdbms that referenced this issue Nov 11, 2013
# See also pull request #273
thekid added a commit to xp-framework/csv that referenced this issue Nov 11, 2013
# See also pull request #273
thekid added a commit to xp-framework/parser that referenced this issue Nov 11, 2013
# See also pull request #273
thekid added a commit to xp-framework/rest that referenced this issue Nov 11, 2013
# See also pull request #273
thekid added a commit to xp-framework/scriptlet that referenced this issue Nov 11, 2013
# See also pull request #273
thekid added a commit to xp-framework/imaging that referenced this issue Nov 12, 2013
# See also pull request #273
thekid added a commit to xp-framework/ftp that referenced this issue Nov 12, 2013
# See also pull request #273
thekid added a commit to xp-framework/http that referenced this issue Nov 12, 2013
# See also pull request #273
thekid added a commit to xp-framework/ldap that referenced this issue Nov 12, 2013
# See also pull request #273
thekid added a commit to xp-framework/mail that referenced this issue Nov 12, 2013
# See also pull request #273
thekid added a commit to xp-framework/news that referenced this issue Nov 12, 2013
# See also pull request #273
thekid added a commit to xp-framework/irc that referenced this issue Nov 12, 2013
# See also pull request #273
thekid added a commit to xp-framework/sieve that referenced this issue Nov 12, 2013
# See also pull request #273
thekid added a commit to xp-framework/webdav that referenced this issue Nov 12, 2013
# See also pull request #273
thekid added a commit to xp-framework/spelling that referenced this issue Nov 12, 2013
# See also pull request #273
thekid added a commit to xp-framework/xml that referenced this issue Nov 12, 2013
# See also pull request #273
thekid added a commit to xp-framework/telephony that referenced this issue Nov 12, 2013
# See also pull request #273
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant