Q: Only Getting 40% Code coverage on Trigger Please Help! I'm trying to update a lookup field on the lead object via trigger, but I'm not getting enough code coverage.  Can someone help me ?
TRIGGER
trigger PartnerName on Lead (before insert) {
Map<string, List <Lead>> contactNamesToLeadsMap = new Map<string, List <Lead>>();

    for(Lead ld : trigger.new){
        if(!String.isBlank(ld.name)) {
            List<Lead> leadsForPartner = null;
            if(contactNamesToLeadsMap.containsKey(ld.name)) {
                leadsForPartner = contactNamesToLeadsMap.get(ld.name);
            } else {
                leadsForPartner = new List<Lead>();
                contactNamesToLeadsMap.put(ld.name, leadsForPartner);
            }
            leadsForPartner.add(ld);
        } else {
            ld.Partner_Name__c = null;
        }
    }

    List <Contact> contlist = [select Id, Name from Contact where Name in :contactNamesToLeadsMap.keySet()];

    for(Contact con : contlist) {


        List<Lead> leadsForPartner = contactNamesToLeadsMap.get(con.Name);
        for(Lead ld : leadsForPartner) {
            ld.Partner_Name__c = con.Id;
        }
    }
}

APEX CLASS
@IsTest
public class LeadPartnerTestClass {
    static testMethod Void LeadPartnerTestClass() {
        //Create an Account
        Account a = new account();
        a.Name = 'Test Account';
        insert a;

        //Create a Contact
        Contact c = new contact();
        c.FirstName = 'Test';
        c.LastName = 'Contact';
        c.AccountId = a.id;
        insert c;

        //Create a "Lead"
        Lead ld = new Lead();
        ld.FirstName= 'Test';
        ld.LastName = 'Contact';
        ld.Company = 'Test Account';
        ld.Partner_Name__c = c.id;
        ld.Status = 'Open';
        insert ld;

        //Query Success
        system.AssertEquals(c.id, ld.Partner_Name__c);
    }
}


Test Class

@IsTest
public class LeadPartnerTestClass {
    static testMethod Void LeadPartnerTestClass() {
        //Create an Account
        Account a = new account();
        a.Name = 'Test Account';
        insert a;

        //Create a Contact
        Contact con = new contact();
        con.FirstName = 'Test';
        con.LastName = 'Contact';
        con.AccountId = a.id;
        insert con;


        //Create a "Lead"
        Lead ld = new Lead();
        ld.FirstName= 'Test';
        ld.LastName = 'Lead';
        ld.Company = 'Test Account';
        ld.PartnerFirstName__c = 'Test';
        ld.PartnerLastName__c = 'Contact';
        ld.Partner_Name__c = con.id;
        ld.Status = 'Open';
        insert ld;

            //Query Success
        system.AssertEquals(con.id, ld.Partner_Name__c);
    }
}

![Code Coverage Screen Shot}(https://app.box.com/s/h2fnibjabfgpn8rvqg7m)
Updated Test Class
@IsTest
public class LeadPartnerTestClass {

    static testMethod Void LeadPartnerTestClass() {
        list<Lead> leadList = new list<Lead>();

        //Create an Account
        Account a = new account();
        a.Name = 'Test Account';
        insert a;

        //Create a Contact
        Contact con = new contact();
        con.FirstName = 'Test';
        con.LastName = 'Contact';
        con.AccountId = a.id;
        insert con;

        //Create a "Lead"
        Lead ld = new Lead();
        ld.FirstName= 'Test';
        ld.LastName = 'Lead';
        ld.Company = 'Test Account';
        ld.PartnerFirstName__c = 'Test';
        ld.PartnerLastName__c = 'Contact';
        ld.Partner_Name__c= con.id;
        ld.Status = 'Open';
        leadList.add(ld);

        Lead ld2 = new Lead();
        ld2.FirstName= 'Test';
        ld2.LastName = 'Lead2';
        ld2.Company = 'Test Account2';
        ld2.PartnerFirstName__c = null ;
        ld2.PartnerLastName__c = null ;
        ld2.Partner_Name__c= con.id;
        ld2.Status = 'Open';
        leadList.add(ld2);

        Lead ld3 = new Lead();
        ld3.FirstName= 'Test';
        ld3.LastName = 'Lead3';
        ld3.Company = 'Test Account3';
        ld3.PartnerFirstName__c = null ;
        ld3.PartnerLastName__c = null ;
        ld3.Status = 'Open';
        leadList.add(ld3);

        insert leadList;

         //Query Success
        system.AssertEquals(con.id, ld.Partner_Name__c);   
    }
}


A: Take a look at Testing Best Practices here, if you focus on use case as well as bulk record processing the code coverage will follow.  

While only 75% of your Apex code must be covered by tests, your focus
  shouldn't be on the percentage of code that is covered. Instead, you
  should make sure that every use case of your application is covered,
  including positive and negative cases, as well as bulk and single
  records. This should lead to 75% or more of your code being covered by
  unit tests.  
  
  
*
  
*In the case of conditional logic (including ternary operators),    execute each branch of code logic.
  
*Make calls to methods using both valid and invalid inputs.
  
*Complete successfully without throwing any exceptions, unless those    errors are expected and caught in a try…catch block.
  
*Always handle all exceptions that are caught, instead of merely    catching the exceptions.
  
*Use System.assert methods to prove that code behaves properly.
  
*Use the runAs method to test your application in different user    contexts.
  
*Exercise bulk trigger functionality—use at least 20 records in your    tests.

If you view which lines are not covered it should give you a better idea of what you are missing. Information on checking coverage can be found here, but as crmprogdev pointed out you need to make sure your tests enter each branch of your if-else statements. For example your test record has a name so the else(ld.Partner_Name__c = null;) in this statement(if(!String.isBlank(ld.name))) will not be covered.  Another example would be this line if(contactNamesToLeadsMap.containsKey(ld.name)) will not be true when your test runs because you are only inserting one record so this line will not be covered either: leadsForPartner = contactNamesToLeadsMap.get(ld.name);

Checking Code Coverage
The Developer Console retrieves and displays code coverage information
  from your organization. Code coverage results come from any tests
  you've run from an API or from a user interface (for example, the
  Developer Console, the Force.com IDE, or the Apex Test Execution
  page). To clear the current results, click Test | Clear Test Data.
  When you edit a class, the code coverage for that class is cleared
  until you run the tests again.
You can view code coverage in several places in the Developer Console.
  
  
*
  
*The Tests tab includes an Overall Code Coverage panel that displays    the code coverage percentage for every Apex class in your
  organization that has been included in a test run. It also displays
  the overall percentage.
  
*Double-click a completed test run to open a Tests Results view that    displays the tested class, the tested method, the duration, result
  (skip, pass, or fail), and an optional error message. If the test
  failed, a Stack Trace column shows the method and line number at
  which the test failed.
  
*To view line-by-line code coverage for an Apex class, open the class.    The Code Coverage menu will include one or more of the
  following    options depending on the tests you have implemented:
-- None
-- All Tests: The percentage of code coverage from all test runs.
-- className.methodName: The percentage of code coverage from a method    executed during a test run.
Lines of code that are covered by tests are blue. Lines of code that
  aren't covered are red. Lines of code that don't require coverage (for
  example, curly brackets, comments, and System.debug calls) are left
  white.  


A: You need to create a separate method for each branch of your if-else statements or one method that has data in it that creates those respective conditions in order to get full coverage on your trigger. 
