Should I be unit testing every piece of code

I recently started unit testing and I wonder if block tests should be written to cover 100% of the code?

It seems useless when I finish writing more unit testing code than production code.

I am writing a PHP Codeigniter project, and sometimes it seems like I am writing so much code to test one small function.

For an example it is Unit test

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;
}

For this production code

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');
    }
}

Where am I so wrong?

+3
source share
2 answers

In my opinion, normal for test code is nothing more than production code. But the test code tends to be simple, as soon as you hang it, it is not an easy task to write tests.

, , / , : ..

, , 100% . , , , . @codeCoverageIgnore, .

+3

, , - , , , .

, 80% . 100% , , , getter. Dont ;)

+2

All Articles