ERPNext Unittest, How to setup?

Hi,

I’ve tried to create simple unittest for custom app.

But when I tried to create Sales Order using make_sales_order, I got LinkValidationError

I know that creating _Test Company, _Test Item and _Test Warehouse - _TC will solve the problem.

My question is
Is there a function or database can be use for creating test data for default ERPNext doctype ?

Error

======================================================================
ERROR: test_transaction_base (custom_app.test.test_money_in_words.TestMoneyInWords)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/frappe/bench/apps/custom_app/custom_app/test/test_money_in_words.py", line 10, in test_transaction_base
    amount='2134.23',
  File "/home/frappe/bench/apps/erpnext/erpnext/selling/doctype/sales_order/test_sales_order.py", line 901, in make_sales_order
    so.insert()
  File "/home/frappe/bench/apps/frappe/frappe/model/document.py", line 224, in insert
    self._validate_links()
  File "/home/frappe/bench/apps/frappe/frappe/model/document.py", line 766, in _validate_links
    frappe.LinkValidationError)
  File "/home/frappe/bench/apps/frappe/frappe/__init__.py", line 377, in throw
    msgprint(msg, raise_exception=exc, title=title, indicator='red', is_minimizable=is_minimizable)
  File "/home/frappe/bench/apps/frappe/frappe/__init__.py", line 356, in msgprint
    _raise_exception()
  File "/home/frappe/bench/apps/frappe/frappe/__init__.py", line 316, in _raise_exception
    raise raise_exception(msg)
frappe.exceptions.LinkValidationError: Could not find Company: _Test Company, Row #1: Item Code: _Test Item, Row #1: Delivery Warehouse: _Test Warehouse - _TC

----------------------------------------------------------------------
Ran 1 test in 1.398s

FAILED (errors=1)

Code

import frappe
import unittest


class TestMoneyInWords(unittest.TestCase):
    def test_transaction_base(self):
        from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order
        so = make_sales_order(
            currency='NGN',
            amount='2134.23',
        )
        self.assertEqual(so.base_rounded_total, 'aaa')
        self.assertEqual(so.grand_total, 'aaa')
1 Like

I’ll :+1: this. Going to start testing some of my custom app. And I don’t see much docs apart from this. Which seems lacking for a huge framework like frappe, to only have one page explaining the basics of testing.

There is a bit more detail doc Frappe - Unit Testing.

But it doesn’t have anything on create test record.

Related issue
https://github.com/frappe/frappe/issues/11269

I meant creating test data for default ERPNext doctype.

So in Sales Order DocType it needs _Test Company, _Test Item and _Test Warehouse - _TC

Is there a function or database can be use for creating test data for default ERPNext doctype ?