您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit组织测试
发布时间:2021-09-20 14:15:26编辑:雪饮阅读()
Phpunit组织测试用于测试多个测试用例,像是比如一个tests目录下有多个类都需要测试这种情况。
function autoload($class){
include $class.'.php';
}
spl_autoload_register('autoload');
User.php:
DatabaseTest.php:
UserTest.php:
运行效果如:
use PHPUnit\Framework\TestCase;
final class Database extends TestCase
{
public function add()
{
return "add success";
}
}
class Errorcode
{
const NAME_IS_NULL = 0;
}
class User
{
public $name;
public function __construct($name)
{
$this->name=$name;
}
public function Isempty()
{
try{
if(empty($this->name))
{
throw new Exception('its null',Errorcode::NAME_IS_NULL);
}
}catch(Exception $e){
return $e->getMessage();
}
return 'welcome '.$this->name;
}
}
use PHPUnit\Framework\TestCase;
final class DatabaseTest extends TestCase
{
public function testAdd(): void
{
$db=new Database();
$result=$db->add();
$this->assertEquals('add success',$result);
}
}
use PHPUnit\Framework\TestCase;
class UserTest extends TestCase
{
protected $user;
public function setUp():void
{
$this->user = new User('');
}
public function testIsempty()
{
$this->user->name='mark';
$result =$this->user->Isempty();
//原型:assertEquals(mixed $expected, mixed $actual[, string $message = ''])
//当两个变量 $expected 和 $actual 不相等时报告错误,错误讯息由 $message 指定。
$this->assertEquals('welcome mark',$result);
$this->user->name='';
$results =$this->user->Isempty();
$this->assertEquals('its null',$results);
}
}
关键字词:phpunit,组织测试
相关文章
- phpunit使用setUpBeforeClass与tearDownAfterClass实
- phpunit生命周期(钩子)assertPreConditions、assertPo
- phpunit类方法运行生命周期
- phpunit基境-setUp
- phpunit使用testdox的testdox-text与testdox-html参数
- phpunit敏捷文档testdox的带参情况
- phpunit使用testdox情况下多个测试方法的名字互相之间
- phpunit中filter的使用(匹配命名空间、类、方法以及数
- phpunit中expectException的使用
- phpunit中NOTICE、WARNING、ERROR的断言支持