Tests fail with DuplicateEntryError: Price List Standard Buying on non-INR sites: cause & workaround

Hi everyone,

If you run ERPNext tests on a site whose currency is not INR (GHS, USD, EUR, and so on), you may have seen bench run-tests crash before any test even starts, with this error:

frappe.exceptions.DuplicateEntryError: ('Price List', 'Standard Buying', IntegrityError(1062, "Duplicate entry 'Standard Buying' for key 'PRIMARY'"))

Here’s what is going on and how to get past it.

Who is affected

Anyone running bench --site <site> run-tests (or anything that imports erpnext.tests.utils) on a site where the Standard Buying or Standard Selling price list uses a currency other than INR.

Tested on Frappe v16.60.2 / ERPNext v16.60.0.

What’s happening, in plain words

Before the tests run, ERPNext sets up some sample data, including a few price lists. For each one it first checks whether it already exists, and creates it only if it doesn’t.

The problem is how it checks. It doesn’t just ask “is there a price list called Standard Buying?” It asks “is there a price list called Standard Buying, in INR, enabled, used for buying?”

On your site, Standard Buying exists but is in GHS (for example). So the answer is “no”, and ERPNext tries to create a new Standard Buying. That name is already taken, so the database refuses and the whole test run stops.

Think of a guest list: you check for “John, wearing blue”. John is there, but in red. So you try to add a second John with the same ID card, and security stops you at the door.

Because this setup runs as soon as erpnext.tests.utils is imported, every test that uses it fails, not only price list tests.

The behaviour came in with erpnext PR #47910 (backported to v16 in #53608).

Issue: Test bootstrap fails with DuplicateEntryError when Standard Buying price list is not in INR · Issue #68352 · frappe/erpnext · GitHub

Workarounds (pick one)

Option A: Use a separate test site (recommended)

Create a fresh site just for tests. The test setup creates the price lists it needs there, and your real data stays untouched.

bench new-site test_site
bench --site test_site install-app erpnext
bench --site test_site set-config allow_tests true
bench --site test_site run-tests --app erpnext

Option B: Temporarily switch the price list currency to INR

Do this only on a test or dev site, never on production.

  1. Open Price List Standard Buying, set Currency to INR, save.
  2. Do the same for Standard Selling if it also isn’t INR.
  3. Run your tests.
  4. Switch the currency back afterwards.

Option C: Small local code patch (until the upstream fix is merged)

In apps/erpnext/erpnext/tests/utils.py, inside make_price_list(), change the last line so it only matches on the name:

-		self.make_records(["price_list_name", "enabled", "selling", "buying", "currency"], records)
+		self.make_records(["price_list_name"], records)

The name is the only thing that has to be unique, so this skips any price list that already exists, whatever its currency. Note that bench update will overwrite this change.

To check it works:

bench --site <site> console
>>> import erpnext.tests.utils

It should import without errors.

Permanent fix

I’ve suggested the same change upstream (match existing price lists on name only). If you’re hitting this too, a :+1: on the issue helps it get noticed: Test bootstrap fails with DuplicateEntryError when Standard Buying price list is not in INR · Issue #68352 · frappe/erpnext · GitHub

Hope this saves someone a few hours of debugging!