News Feed
Sections




News Archive
feed this:

Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Matthias Noback:
A better PHP testing experience Part I Moving away from assertion-centric testing
July 07, 2014 @ 17:53:45

Matthias Noback has a new post today, part one in a series, looking at moving away from assertion-centric testing and more towards descriptive unit tests.

In the introduction article of this series I quickly mentioned that I think unit testing often focuses too much on assertions. [...] I used to preach these things myself too (yes, "development with tests" often comes with a lot of preaching). But now I don't follow these rules anymore. I will shortly explain my reasons. But before I do, let's take a step back and consider something that is known as the Test framework in a tweet, by Mathias Verraes.

He breaks up the rest of the article into three sections, each with a few paragraphs of description and code where appropriate:

  • Something is the case, or not
  • Only one assertion for each test?
  • Write assertions first and then work towards them?

He talks about the idea of "Arrange Act Assert" and makes some recommendations as to how to be more descriptive in your tests and make them easier to read and follow in the future.

0 comments voice your opinion now!
unittest experience assertion testing descriptive

Link: http://php-and-symfony.matthiasnoback.nl/2014/07/descriptive-unit-tests/

Matthias Noback:
Test Symfony2 commands using the Process component and asynchronous assertions
March 24, 2014 @ 10:49:13

Matthias Noback has a new post today showing you how to test Symfony2 commands that use the Process component. More specifically, his tests revolve around the ones that use asynchronous assertions in the testing to remove weird behaviors that could be caused by multiple processes running them simultaneously.

Usually you would test a console command using the ConsoleTester class as described in the official Symfony documentation. But we can not use it in this case. We need to isolate the process in order for pcntl_fork() to work correctly, otherwise the PHPUnit test runner itself will be forked too, which will have the effect of running all the following unit tests twice (which can have very strange effects on the test results).

He shows how to use the Process component to start up a new process (the daemon) and check that the PID file exists. He includes an example of a "probe" to determine what processes are running and preventing them from stepping on each other.

0 comments voice your opinion now!
symfony2 command process component asynchronous assertion unittest phpunit

Link: http://php-and-symfony.matthiasnoback.nl/2014/03/test-symfony2-commands-using-the-process-component-and-asynchronous-assertions/

NetTuts.com:
Expressive Tests with Hamcrest
December 06, 2012 @ 12:25:12

On the NetTuts.com site today there's a new tutorial introducing the Hamcrest validation matchers and how to use them in your PHPUnit testing to enhance both the readability and functionality of the assertions.

Hamcrest is a set of matchers for writing more expressive code. It just so happens that these matchers are especially useful when writing tests. In this article, we'll look at Hamcrest for PHP. [...] Hamcret's expressiveness originated with JMock, but it wasn't until the addition of the unique assertThat() method that it was refactored into a self-contained library and independently usable in testing frameworks.

He talks about the "generations" of unit testing tools, a division based on their assertion functionality - simple, exact or using matchers. Installation instructions via PEAR are included (though there's also a composer package for it too) as well as code for an example test. More samples are given for comparing things like numeric values, strings and setting up inclusions and exclusions with the anyOf/noneOf matchers.

0 comments voice your opinion now!
hamcrest unittest assertion tutorial library


PHPFreaks.com:
PCRE Regex Spotlight K
August 25, 2009 @ 10:28:44

On the PHPFreaks.com blog today there's a quick new post looking at one of the special backslash strings that doesn't get talked about very much - K - but is quite powerful.

One backslash sequence that doesn't get much attention is K. What this handy little assertion does is match the position of whatever string comes before it in the pattern, then it in essence resets the match. At that point it starts a new match with whatever comes after K from the current location in the subject string.

They include a series of PHP code examples showing how use can use it to work around some of the issues with lookbehind assertions. They also include a few benefits and drawbacks of using them over lookbehinds.

0 comments voice your opinion now!
regularexpression regex assertion backslashk


Ralph Schindler's Blog:
Dynamic Assertions for Zend_Acl in ZF
August 17, 2009 @ 12:37:17

Ralph Schindler has a new post to his blog today looking at using dynamic assertions with the access control component (Zend_Acl) of the Zend Framework.

Over the last two years, I've seen a variety of duplicate issues come into the issue tracker, which stem from two fundamental flaws in Zend_Acl [...] In this article, we'll explore the API changes that alleviate these two problems, and we'll demonstrate how to leverage the Zend_Acl assertion system to create expressive, dynamic assertions that work with your applications models.

He mentions some of the backwards compatible changes that have been made to the ACL API including changes in the add() method and the ability to create Zend_Acl_Role and Zend_Acl_Resource objects explicitly. The rest of the post gives a great example of setting up users in a role, creating an action to test them against (can they work with a blog post?) and running a series of checks against the ACL component as a guest, contributor and publisher.

3 comments voice your opinion now!
zendframework dynamic assertion zenacl


PHP in Action Blog:
One behavior != one assertion
February 24, 2009 @ 11:13:17

On the PHP in Action blog, despite some agreement with Padraic Brady on his one behavior, one assertion method for writing unit tests, Dagfinn Reiersol still has his reservations.

Pádraic maintains that one assertion per test is a rule that should always be followed unless there is a specific good reason to break it. I prefer it as a guideline, as does Robert C. Martin in the book Clean Code. The reference is not intended as an appeal to authority to "prove" that I'm right. I'm just making the point that I think this reflects the current state of the art, which is not necessarily perfect, of course.

Dagfinn talks about behavior-driven development and illustrates how a two assertions could be combined into one but that the end result "isn't very pretty" - and that's just testing two proterties of a single object. Imagine what would happen if things got more complex.

0 comments voice your opinion now!
unittest behavior assertion reservation opinion padraicbrady


Padraic Brady's Blog:
Unit Testing One Test, One Assertion - Why It Works
February 17, 2009 @ 07:56:09

Padraic Brady has pormoted simplicity for unit testing in his latest blog post. He suggests that, for each test in your suite, you have only one assertion inside.

Obviously, a ratio of 1:1 would be perfect - but I'm sure we can accept some multiple assertions within a tolerance level. A ratio of more than 1.1:1 would indicate creeping obscurity (a signal it's time for me to intervene, refactor, and do some mutation testing for extra assurance). A ratio of 2:1 indicates a lost cause.

He notes that it probably sounds like a bad idea from the start, but he reinforces the suggestion with a bit of reasoning:

It's this notion of complexity that is often used to justify multiple assertions - if you break it down into the simple components that that complexity was built out of, multiple assertions don't have a leg to stand on (just the rare instances out of your control because they absolutely must be done).

You can see response that's been posted over on the PHP in Action blog in response to Padraic's thoughts.

1 comment voice your opinion now!
unittest assertion test reason opinion multiple phpunit tdd testdrivendesign


Padraic Brady's Blog:
Unit Testing Multiple Assertions And Lazy/Shallow Testing Are Evil
February 13, 2009 @ 09:31:33

In a new post today Padraic Brady takes a look at unit testing and two of the bad practices that can develop over time when writing your tests - overloading tests with assertions and lazy/shallow tests.

In short, every test you write requires that you setup the test environment, create a scenario for possible failure, add an assertion, and then ensure the source code makes that assertion pass. This requires code - sometimes a lot of code. So adding multiple assertions to each test minimizes the work needed to write tests, since using multiple assertions takes advantage of existing code to avoid writing new stuff to clutter your test classes. It can also help to tackle multiple but related results in the same test.

He gives brief examples of both instances and some of the problems associated with them. Multiple assertions, while nice for fine tuning the results of the testing, can also confuse since, if one assertion fails, the whole test fails too. The other end of the spectrum is a problem too - writing tests that don't do enough to really check the information passed in. These shallow tests can lead to issues down the line if unanticipated data might come along.

Really, its all about finding that "sweet spot" in developing tests - not too much, not too little - and keeping it simple while not missing out on assertions that need to be made.

0 comments voice your opinion now!
unittest phpunit lazy shallow multiple assertion example


Raphael Stolt's Blog:
Creating custom PHPUnit assertions
July 29, 2008 @ 10:25:15

Raphael Stolt has written up a post for his blog talking about custom PHPUnit assertions and a simple method to create them.

In this blog post I'd like to set the focus on some of the aspects of the Custom Assertion pattern, by showing how to create custom PHPUnit assertions, which attacks the above mentioned smell [of bad test code] and its retroactive effects with a huge antiperspirant flagon, while also providing the chance to build a customer friendly and domain related test vocabulary.

His first assertion is simple, checking the contents of a bag object to be sure that there are no duplicate items and that its content count is reduced by one if something is removed. Then, he moves on to making the custom assertion for his "IdShouldFollowAgreedConvention" test. He explains how to use the assertion class and how it all fits together in the final, functional test case.

0 comments voice your opinion now!
custom phpunit assertion unittest customassertion pattern


Felix Geisendorfer's Blog:
Assert the yummyness of your cake
October 25, 2007 @ 12:16:00

Felix Geisendorfer has posted an addition to his previous look at exceptions in CakePHP with a modification that uses assertions instead of an if to check the value of a variable.

I suddenly remembered what assertions are and how they could make my code more readable, while making it shorter. I took a quick look at PHP's assert function, but decided that I don't like it. [...] What I ended up doing was basically to create a class called Assert, and abuse it as a name-space for a whole bunch of useful assertion functions.

He includes two examples of how this new class would look, checking to see if a value is empty and to see if a task ID exists in a given value. The code for the assertion class is also included and has methods for checking things like "not empty", "equals to", "is not numeric" and "is an object". And, no, it's not CakePHP specific, so you could take it an use it anywhere.

1 comment voice your opinion now!
cakephp assertion class exception addition cakephp assertion class exception addition



Community Events





Don't see your event here?
Let us know!


voicesoftheelephpant framework tool series laravel language interview podcast composer security conference community list artisanfiles release library opinion version symfony introduction

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework