Our site imports customers from external data sources. Part of this import is creating a default billing address from external data if none exists.

I have followed advice from other questions, as wells as tutorials, all of which seem to say the same thing for creating/saving a default billing address.
This seems to work at least partially.

// $c is the customer
$data_source  = array(
    'street'   =&gt; '',
    'city'     =&gt; '',
    'postcode' =&gt; '',
);

$a = $c-&gt;getPrimaryBillingAddress();

// Create a new default billing if none found
if (!$a)
{
  $a = Mage::getModel("customer/address");
  $a-&gt;setCustomerId($c-&gt;getId())
    -&gt;setIsDefaultBilling('1')
    -&gt;setSaveInAddressBook('1');
}

foreach ($data_source as $k =&gt; $d) $a-&gt;setData($k, $d);

$a-&gt;save();


After this code has been run, the address is visible on the customer's page in the backend and is marked as the default billing.

However, the following time data is fetched from our external source and the code is run,
the address that was saved is not retrieved (getPrimaryBillingAddress returns false).
This results in a new address being created every time this code is run.

The exception is when a customer has a default billing address set by magento.
In this case, getPrimaryBillingAddress correctly finds the address and the address data is updated and saved.

How can I programatically create a default billing address in such a way that I can retrieve it with $customer-&gt;getPrimaryBillingAddress()?

UPDATE

After receiving a possible answer, I have adjusted to code to set customer's default_billing.
The main problem persists with the changes.

$a = $c-&gt;getPrimaryBillingAddress();

// Create a new default billing if none found
if (!$a)
{
  $a = Mage::getModel("customer/address");
  $a-&gt;setCustomerId($c-&gt;getId())            // &lt;-- variations of customer/customerID 
    -&gt;setCustomer($c)                       //     don't seem to affect
    -&gt;setIsDefaultBilling('1')
    -&gt;setSaveInAddressBook('1')
    -&gt;save();                               // &lt;--- SAVE SO WE CAN ACCESS ID

  $c-&gt;addAddress($a)                        // &lt;--|
    -&gt;setDefaultBilling($a-&gt;getId())        // &lt;--+ SAVE default_billing IN CUSTOMER
    -&gt;save();                               // &lt;--|

}

foreach ($data_source as $k =&gt; $d) $a-&gt;setData($k, $d);

$a-&gt;save();

// ---------------- SUBSEQUENT RUNS --------------

$c-&gt;getDefaultBilling();                    // null
$c-&gt;getPrimaryBillingAddress();             // false


References

Variations on this method for creating/setting a customer default address were found at these locations:


SO - Magento : Add customer default billing address
Programatically create customer and order in Magento with full blown One Page Checkout process under the hood



Question: How do I know I'm running within "go test"
Answer: Code examples would help. But from your question it sounds like you've hardcoded a path to a config file somewhere when you probably wanted to pass it in instead.

Change the function you are testing to take a parameter defining the config file and then in your test code pass a different path in than you use in the non test code. It's bad practice for your code to have a different path when testing vs production.