Assertions in real life

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am told that I should use assertions in my testing. The problem is,
because I test using a cut of live data, the data always changes. Has
anyone come up against this or have they abandoned assertions generally?

On a more general note, how does one reconcile a changing data set with
testing driven development?

Re: Assertions in real life

Hugh Oxford wrote:
Quoted text here. Click to load it

You need to validate external data.  Whether you use assertions or
another means is up to you.  But the data do need to be validated.

You should know what data are valid, and what are invalid, and need to
verify that valid data are accepted and invalid data rejected by your

This isn't just in PHP - it's in any program and language.  It's just
good programming practice.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: Assertions in real life

Hej Hugh,

for unit tests assertions are a common thing.

In case you don't know what unit tests are, please read about them. Look
for "simpletest" or "phpunit".

Normally tests should provide their own data to run against the unit to
be tested.

If this doesn't fit to your problem, provide more information.


Re: Assertions in real life

On Tue, 18 Nov 2008 22:51:01 +0000, wrote:
Quoted text here. Click to load it

Assertions are not meant to be a form of error handling, and
generally should never be present in production code.  They aren't
necessary, but can sometimes be helpful for sanity checks.

Assertions can be helpful when doing something like testing some
expression that, logically, should not fail.  Take another example:
in C programming, it can be helpful to use assertions to ensure you
don't try to dereference NULL pointers (you should never do this),
thus making it easier to catch undefined behavior, letting you know
exactly where the problem occurred, should the assertion fail.

Quoted text here. Click to load it

This seems to be a bit of a vague question.  You should have an idea
of the type of data your application needs.  You should then take
necessary steps to validate and/or sanitize the data appropriately.  
Make sure to only escape data appropriately during output as well.  
It's often easier to store raw data, and only escape as necessary.

$email = str_replace('sig.invalid', '', $from);

Site Timeline