Unit Test Failure Because Document Doesn't Exist But It Does

I am attempting to write a unit test against Payroll Entry because I would like to be able to run a bunch of tests every year after I have updated the Salary Components for a US based system. US payroll is a little complex and I really want to test the formulas before I push to production.

I have my unit test going through and creating the employees, have all the Salary Components, Salary Structure, etc. I go to create the Payroll Entry and I’m able to save it but then it fails when I try to submit. This is the last step of course because I want to get at the Salary Slips and compare the withholding values to test my formulas.

Here is the error it’s returning:

======================================================================
ERROR: test_payroll (business.paycheck.tests.test_payroll.TestPayroll.test_payroll)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/work/frappe/apps/business/business/paycheck/tests/test_payroll.py", line 293, in test_payroll
    payrollEntry.submit()
  File "/work/frappe/apps/frappe/frappe/utils/typing_validations.py", line 31, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/work/frappe/apps/frappe/frappe/model/document.py", line 1048, in submit
    return self._submit()
           ^^^^^^^^^^^^^^
  File "/work/frappe/apps/frappe/frappe/model/document.py", line 1031, in _submit
    return self.save()
           ^^^^^^^^^^^
  File "/work/frappe/apps/frappe/frappe/model/document.py", line 337, in save
    return self._save(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/work/frappe/apps/frappe/frappe/model/document.py", line 390, in _save
    self.run_post_save_methods()
  File "/work/frappe/apps/frappe/frappe/model/document.py", line 1131, in run_post_save_methods
    self.run_method("on_submit")
  File "/work/frappe/apps/frappe/frappe/model/document.py", line 962, in run_method
    out = Document.hook(fn)(self, *args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/work/frappe/apps/frappe/frappe/model/document.py", line 1322, in composer
    return composed(self, method, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/work/frappe/apps/frappe/frappe/model/document.py", line 1304, in runner
    add_to_return_value(self, fn(self, *args, **kwargs))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/work/frappe/apps/frappe/frappe/model/document.py", line 959, in fn
    return method_object(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/work/frappe/apps/hrms/hrms/payroll/doctype/payroll_entry/payroll_entry.py", line 65, in on_submit
    self.create_salary_slips()
  File "/work/frappe/apps/frappe/frappe/utils/typing_validations.py", line 31, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/work/frappe/apps/hrms/hrms/payroll/doctype/payroll_entry/payroll_entry.py", line 257, in create_salary_slips
    self.reload()
  File "/work/frappe/apps/frappe/frappe/model/document.py", line 209, in reload
    return self.load_from_db()
           ^^^^^^^^^^^^^^^^^^^
  File "/work/frappe/apps/frappe/frappe/model/document.py", line 172, in load_from_db
    frappe.throw(
  File "/work/frappe/apps/frappe/frappe/__init__.py", line 658, in throw
    msgprint(
  File "/work/frappe/apps/frappe/frappe/__init__.py", line 623, in msgprint
    _raise_exception()
  File "/work/frappe/apps/frappe/frappe/__init__.py", line 574, in _raise_exception
    raise exc
frappe.exceptions.DoesNotExistError: Payroll Entry HR-PRUN-2025-00038 not found

----------------------------------------------------------------------

Tracing back in the hrms code in payroll_entry.py, the code that is being run is just a simple reload of the Payroll Entry document (self.reload()). This is turn is running self.load_from_db().

I’m by no means an extra in unit testing in Frappe. From what I can tell, all data generated during the tests are not inserted into the database. So I’m thinking that self.load_from_db() is going straight to the database but since this is a unit test, it can’t find it and is failing. Is my thinking correct?

Does anyone have any guidance around how I could work around this and get this test to work?

Looking in the test_payroll.py file from the hrms app, I found that they are doing a database commit:

frappe.db.commit()

So I’m inferring now that during testing, the database is not auto committing, the transactions are taking place, and then a rollback is done at the end of testing. But for this situation, a commit has to be done in order to get it to work.

So if I use commit, is there an easy way to clean up after the test concludes or do I just need to go through and call deletes for everything that was created?