Sunday 15 July 2012

php - PHPUnit how to test for two conditions -


I am new to using PHPUnit, I discovered that if the value assertEquals Function But I'm not sure how to test for values ​​with more than one condition such as this:

  function myfunction ($ foo, $ bar, $ Baz) {if (($ foo = 3) and ($ foo = 5)} {// some} if (($ bar & lt ;!) or ($ bar> 10)) {// something } If ((strlen ($ baz) === 0) or (strlen ($ baz)> 10)) {// some}}   

Can anyone help in eating? Thank you for your help in advance

A test case for every possible path of each method / function In your application, in your example, you have two possible cases for the first conditional, when $ foo is different from 5 and is different from 5 and when $ foo equals 3 or 5. First you create two test cases:

  & lt ;? Php class YourClassTest extends PHPUnit_Framework_Testcase {public function test_when_foo_is_different_to_three_or_five () {$ this- & gt; AssertEquals ('expected result when foo is different from 3 or 5', myfunction (1)); } Public function test_when_foo_is_equal_to_three_or_five () {$ expected = 'expected result when foo = 3 or foo = 5'; $ This- & gt; Emphasis value ($ expected, myfunction (3)); $ This- & gt; Emphasizing value ($ expected, myfunction (5)); }}   

Now you should do this for the rest of the terms and permutations. Although you feel that the realization of myfunction () method also tests many things and it is difficult and therefore you understand that you should have all your conditional movements for different methods and use them different tests, and after that a good search () by myfunciton desired order to call them to consider that the approach you must absolutely:

  function myFunction ($ foo, $ bar, $ baz) { DoSomethingWithFoo ($ f Oo); DoSomethingWithBar ($ bar); DoSomethingWithBaz ($ baz); } Function doSomethingWithFoo ($ foo) {if (($ foo = 3) and ($ foo = 5)!) {// something}} function DoSomethingWithBar ($ bar) {if ($ (time & lt; 1) or ($ bar & gt; 10)) {// something}} function doSomethingWithBaz ($ baz) {if ((strlen ($ baz) === 0) or (strlen ($ baz) & gt; 10)) {/ / Some}}   

The test will help you with this kind of refactoring. Hope that with this help you can make a bit more clear.

No comments:

Post a Comment