天天看點

assert執行php,PHP:如何使用assert

按合同設計

我相信斷言在PHP中用得不多.你可以使用它,如果你Design By Contract.

Design by Contract (DbC) or

Programming by Contract is an approach

to designing computer software. It

prescribes that software designers

should define formal, precise and

verifiable interface specifications

for software components, which extend

the ordinary definition of abstract

data types with preconditions,

postconditions and invariants. These

specifications are referred to as

“contracts”, in accordance with a

conceptual metaphor with the

conditions and obligations of business

contracts.

這個tutorial解釋了一下.

TDD /單元測試

我建議你按照這些3 simple rules(必讀文章)對你的代碼(TDD)進行單元測試.

You are not allowed to write any production code unless it is to make a

failing unit test pass.

You are not allowed to write any more of a unit test than is sufficient

to fail; and compilation failures are

failures.

You are not allowed to write any more production code than is

sufficient to pass the one failing

unit test.

You must begin by writing a unit test

for the functionality that you intend

to write. But by rule 2, you can’t

write very much of that unit test. As

soon as the unit test code fails to

compile, or fails an assertion, you

must stop and write production code.

But by rule 3 you can only write the

production code that makes the test

compile or pass, and no more.

If you think about this you will

realize that you simply cannot write

very much code at all without

compiling and executing something.

Indeed, this is really the point.

要練習(訓練),你應該使用優秀的PHPUnit架構.你應該閱讀這篇Writing Tests for PHPUnit以了解這門學科.