我应该对每一段代码进行单元测试吗

EdL*_*EdL 5 php unit-testing codeigniter

我最近开始进行单元测试,我想知道我是否应该编写 100% 代码覆盖率的单元测试?

当我最终编写的单元测试代码多于生产代码时,这似乎是徒劳的。

我正在编写一个 PHP Codeigniter 项目,有时我似乎编写了很多代码只是为了测试一个小函数。

例如这个单元测试

public function testLogin(){
    //setup
    $this->CI->load->library("form_validation");
    $this->realFormValidation=new $this->CI->form_validation;
    $this->CI->form_validation=$this->getMock("CI_Form_validation");
    $this->realAuth=new $this->CI->auth;
    $this->CI->auth=$this->getMock("Auth",array("logIn"));
    $this->CI->auth->expects($this->once())
                   ->method("logIn")
                   ->will($this->returnValue(TRUE));

    //test
    $this->CI->form_validation->expects($this->once())
        ->method("run")
        ->will($this->returnValue(TRUE));
    $_POST["login"]=TRUE;
    $this->CI->login();
    $out = $this->CI->output->get_headers();
    //check new header ends with dashboard
    $this->assertStringEndsWith("dashboard",$out[0][0]);

    //tear down
    $this->CI->form_validation=$this->realFormValidation;
    $this->CI->auth=$this->realAuth;

}
public function badLoginProvider(){
    return array(
        array(FALSE,FALSE),
        array(TRUE,FALSE)
    );
}
/**
 * @dataProvider badLoginProvider
 */
public function testBadLogin($formSubmitted,$validationResult){
    //setup
    $this->CI->load->library("form_validation");
    $this->realFormValidation=new $this->CI->form_validation;
    $this->CI->form_validation=$this->getMock("CI_Form_validation");

    //test
    $this->CI->form_validation->expects($this->any())
        ->method("run")
        ->will($this->returnValue($validationResult));
    $_POST["login"]=$formSubmitted;
    $this->CI->login();
    //check it went to the login page
    $out = output();
    $this->assertGreaterThan(0, preg_match('/Login/i', $out));

    //tear down
    $this->CI->form_validation=$this->realFormValidation;
}
Run Code Online (Sandbox Code Playgroud)

对于这个生产代码

public function login(){
    if($this->input->post("login")){
        $this->load->library('form_validation');
        $username=$this->input->post('username');
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', "required|callback_userPassCheck[$username]");
        if ($this->form_validation->run()===FALSE) {
            $this->load->helper("form");
            $this->load->view('dashboard/login');
        }
        else{
            $this->load->model('auth');
            echo "valid";
            $this->auth->logIn($this->input->post('username'),$this->input->post('password'),$this->input->post('remember_me'));
            $this->load->helper('url');
            redirect('dashboard');
        }
    }
    else{
        $this->load->helper("form");
        $this->load->view('dashboard/login');
    }
}
Run Code Online (Sandbox Code Playgroud)

我哪里错了?

hid*_*dro 3

在我看来,测试代码多于生产代码是很正常的。但测试代码往往很简单,一旦掌握了它的窍门,编写测试就像是一个不费吹灰之力的任务。

话虽如此,如果您发现测试代码太复杂而无法编写/无法覆盖生产代码中的所有执行路径,那么这是进行某些重构的一个很好的指标:您的方法可能太长,或者尝试做几件事,或者有这么多的外部依赖项等等......

另一点是,测试覆盖率高是好事,但不需要是 100% 或某个非常高的数字。有时有些代码没有逻辑,例如简单地将任务委托给其他人的代码。在这种情况下,您可以跳过测试它们并使用@codeCoverageIgnore注释在代码覆盖率中忽略它们。