Skip to content

test: run shared setup once per class to speed up lending tests#1297

Draft
Nihantra-Patel wants to merge 7 commits into
frappe:developfrom
Nihantra-Patel:speed-up-lending-tests-setupclass
Draft

test: run shared setup once per class to speed up lending tests#1297
Nihantra-Patel wants to merge 7 commits into
frappe:developfrom
Nihantra-Patel:speed-up-lending-tests-setupclass

Conversation

@Nihantra-Patel

@Nihantra-Patel Nihantra-Patel commented Jun 24, 2026

Copy link
Copy Markdown
Member

What this does

Moves the shared test setup (loan accounts, loan products, securities and customers) from setUp into setUpClass across the lending test suites.

Why

setUp runs before every single test. The lending tests were rebuilding the same master data again and again for each test in a class, even though the data never changed between tests. This was a big part of why each test took several seconds.

setUpClass runs only once per test class, so the heavy setup happens one time instead of once per test.

What stays the same

  • Each test still gets its fresh applicant lookup in setUp where it is needed.
  • The base test case rolls back the database after every test, so tests stay isolated and do not affect each other.
  • Test logic and assertions are unchanged.

Result

  • Smaller test classes are about 40-50% faster.

Move the master data setup (loan accounts, products, securities and
customers) from setUp to setUpClass in the lending test suites. Before,
this setup ran again before every single test in a class, even though
the data is the same for all of them.

Now it runs only once per test class. Each test still gets a fresh
applicant lookup in setUp where needed, and the per-test rollback from
the base test case keeps the tests isolated.

This cuts the run time of the smaller test classes by about 40-50%
(for example TestLoanInterestAccrual went from about 23s to 13s). The
heavier tests are unchanged because their time is real work, not setup.
@codecov-commenter

codecov-commenter commented Jun 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.59%. Comparing base (b38ce3f) to head (04c92a3).
⚠️ Report is 40 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1297      +/-   ##
===========================================
+ Coverage    82.54%   82.59%   +0.05%     
===========================================
  Files          154      154              
  Lines        10846    10929      +83     
===========================================
+ Hits          8953     9027      +74     
- Misses        1893     1902       +9     
Files with missing lines Coverage Δ
...type/bulk_repayment_log/test_bulk_repayment_log.py 100.00% <100.00%> (ø)
lending/loan_management/doctype/loan/test_loan.py 99.21% <100.00%> (+<0.01%) ⬆️
...nt/doctype/loan_adjustment/test_loan_adjustment.py 100.00% <100.00%> (ø)
...octype/loan_disbursement/test_loan_disbursement.py 100.00% <100.00%> (ø)
...oan_interest_accrual/test_loan_interest_accrual.py 100.00% <100.00%> (ø)
...management/doctype/loan_refund/test_loan_refund.py 100.00% <100.00%> (ø)
...ment/doctype/loan_repayment/test_loan_repayment.py 100.00% <100.00%> (ø)
...oan_repayment_repost/test_loan_repayment_repost.py 100.00% <100.00%> (ø)
.../doctype/loan_restructure/test_loan_restructure.py 100.00% <100.00%> (ø)
...curity_assignment/test_loan_security_assignment.py 100.00% <100.00%> (ø)
... and 4 more

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Nihantra-Patel and others added 6 commits June 24, 2026 16:32
Some loan repayment tests ran interest accrual and daily demand
processing for more months than the test actually paid against or
checked. The extra months only added run time without changing what the
test verifies.

Reduce the processing span to the last month that is used in each test:
- test_in_between_payments and test_in_between_cancellations: 4 -> 3
- test_bulk_payments, test_bulk_repayment_logs and
  test_bulk_payments_for_multiple_disbursements: 6 -> 5

All assertions and paid amounts are unchanged. The tests still pass and
run noticeably faster (for example test_bulk_payments dropped from about
60s to about 25s locally).
The CI MariaDB container holds a disposable test database, so its
durability guarantees are not needed. Relax them to avoid disk I/O that
dominates the test run time:

- innodb-flush-log-at-trx-commit=0: no fsync on every commit (biggest win)
- sync-binlog=0: skip binary-log syncs
- innodb-doublewrite=0: skip the doublewrite buffer
- mount the data dir on tmpfs (RAM) so the test DB does no disk I/O

These mirror the tuning already used by frappe and erpnext CI. They are
safe only because the database is recreated for every run.
test_bulk_repayment_logs and test_bulk_payments_for_multiple_disbursements
assert on Bulk Repayment Log status, not on Loan Repayment behaviour, so
they belong with the Bulk Repayment Log doctype rather than in the already
large loan repayment test file.

Move both into bulk_repayment_log/test_bulk_repayment_log.py (with the same
shared class setup). No test logic changes; both still pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants