php - Types of testcases in PHPUnit -
i new phpunit, infact started today. and, far have been reading, came understand script does.
class usertest extends phpunit_framework_testcase { protected $user; // test talk method protected function setup() { $this->user = new user(); $this->user->setname("tom"); } protected function teardown() { unset($this->user); } public function testtalk() { $expected = "hello world!"; $actual = $this->user->talk(); $this->assertequals($expected, $actual); } }
for class:
<?php class user { protected $name; public function getname() { return $this->name; } public function setname($name) { $this->name = $name; } public function talk() { return "hello world!"; } }
ok, have established test returns ok/fail statement based on equality of test, what looking for more. need ways test more complex class that, outcome, un-like in example can not guessed easily.
say, write script polling. how would, or in ways can test if methods/classes can work? above code shows if methods outcome 'hello world'
but, easy thing test, because need test complex things , there aren't tutorials that.
testing class in unit test supposed not complex.
and reason true in designed system, can test class in isolation, , not add complexity of system behind class - because system not exist during test, mocked.
the classic example user class asks database info, in test case, database hard set up, prepare data, , destroy afterwards. , using real database slows things down. design user class accept database object outside, , in test case give mock object of database.
that way, simple simulate kinds of return values database, , additionally can check whether database object gets right parameters without complexity of dealing real database.
you can proper mock object injection if classes designed allow dependency injection. principle not create objects inside of other objects, require outside world supply them. have quick @ video explanation:
and remember creating tests needs experience. start experimenting.
Comments
Post a Comment