Unit Test for App

100 views
Skip to first unread message

Nguyen Do Le Bao

unread,
Aug 1, 2014, 12:35:24 AM8/1/14
to erpnext-dev...@googlegroups.com
Hi guys, 

I have seen all the unit tests written in the source code. 
Used to write these under GAE as well but still wondering if you guys have any quick guide to write and run those tests ? 

How could I use db stub ? I am still using the old https://github.com/frappe/frappe-bench, would it matter ?

Thanks a lot!

Rushabh Mehta

unread,
Aug 1, 2014, 7:22:28 AM8/1/14
to erpnext-dev...@googlegroups.com
--
Note:
 
If you are posting an issue,
 
1. ERPNext is a free and open source software and support is given on this forum by a team (https://frappe.io/webnotes). So please consider donating if you find this forum useful (https://frappe.io/buy). Even a small amount would be helpful.
2. We should be able to replicate it at our end. So please give us as much information as you can. Please see it from the point of view of the person receiving the communication.
3. Paste your code at http://pastebin.com or http://gist.github.com and send only the URL via email
4. For sending images, use http://imgur.com or other similar services. Do not send images as attachments. Links are good. Same goes for any file you are going to send.
 
End of Note
---
You received this message because you are subscribed to the Google Groups "ERPNext Developer Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/8e4c7b8a-b45c-41a7-95b7-9115f82b6dad%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nguyen Do Le Bao

unread,
Aug 3, 2014, 4:57:08 AM8/3/14
to erpnext-dev...@googlegroups.com
Sure, will test it out and add on to it if there is anything!

Can I check with you something ? Is there a way to run a test server on a Mac OS X ? 
I am developing using a Mac OS X but testing using a Ubuntu server. 

If we can test on the development environment, that will be much faster then transfer the file to server and run.
Thank you!

On Friday, August 1, 2014 7:22:28 PM UTC+8, rushabh wrote:
On 01-Aug-2014, at 10:05 am, Nguyen Do Le Bao <natha...@gmail.com> wrote:

Hi guys, 

I have seen all the unit tests written in the source code. 
Used to write these under GAE as well but still wondering if you guys have any quick guide to write and run those tests ? 

How could I use db stub ? I am still using the old https://github.com/frappe/frappe-bench, would it matter ?

Thanks a lot!

--
Note:
 
If you are posting an issue,
 
1. ERPNext is a free and open source software and support is given on this forum by a team (https://frappe.io/webnotes). So please consider donating if you find this forum useful (https://frappe.io/buy). Even a small amount would be helpful.
2. We should be able to replicate it at our end. So please give us as much information as you can. Please see it from the point of view of the person receiving the communication.
3. Paste your code at http://pastebin.com or http://gist.github.com and send only the URL via email
4. For sending images, use http://imgur.com or other similar services. Do not send images as attachments. Links are good. Same goes for any file you are going to send.
 
End of Note
---
You received this message because you are subscribed to the Google Groups "ERPNext Developer Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer-forum+unsub...@googlegroups.com.

Rushabh Mehta

unread,
Aug 4, 2014, 1:12:54 AM8/4/14
to erpnext-dev...@googlegroups.com
frappe --serve will do it!

I develop and test on a MacOS too.


To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/809cd357-130d-468c-88d6-ab81da84d408%40googlegroups.com.

Nguyen Do Le Bao

unread,
Aug 12, 2014, 2:59:06 AM8/12/14
to erpnext-dev...@googlegroups.com
Cool, thanks a lot!

I followed the new bench setup and got it working on Mac. Wasn't able to do it with the previous frappe version, forgot why...
Btw, when you guys wrote the code for frappe APIs, did you have any unittest code for those ?

I am writing custom API code and wanna test it. The problem is more on setup the frappe.local, cos i make use of that quite heavily
If there are already test for the original APIs, I can just take a look and imitate the setup :D. It will be great!!! 

Thank you again!

Nguyen Do Le Bao

unread,
Aug 12, 2014, 4:33:25 AM8/12/14
to erpnext-dev...@googlegroups.com
I do have another question: 
It seems like during these unit test, it does not connect to the test_db ? Cos it cannot recognise the custom field that I have added into the db.
Is there a way to add those in on the fly during setUp

Thanks!

Rushabh Mehta

unread,
Aug 12, 2014, 5:54:46 AM8/12/14
to erpnext-dev...@googlegroups.com
Setup custom fields as "fixtures" in hooks


To export:

frappe --export_fixtures

They will be installed / updated automatically (on --latest)




To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/581ce5dd-5ddc-43de-87eb-ab2853321dac%40googlegroups.com.

Nguyen Do Le Bao

unread,
Aug 13, 2014, 6:14:26 AM8/13/14
to erpnext-dev...@googlegroups.com
Yeah, that works but the fixtures include all the data records as well. If there is no records, however, the csv file won't contain the custom field.

I am trying to set up mock data for the test, but it is really a pain cos of all the validation links... In order to add Item, I need to have Supplier -> Supplier Type ...... Is there a way to bypass the validations cos in the end that's not what I wanna test ?

This is the code @@: 

I actually wanna clear the db after finishing testing ... but still very messy trying to find a better way to test...

Thanks!



Rushabh Mehta

unread,
Aug 13, 2014, 11:50:00 AM8/13/14
to erpnext-dev...@googlegroups.com
On 13-Aug-2014, at 3:44 pm, Nguyen Do Le Bao <natha...@gmail.com> wrote:

Yeah, that works but the fixtures include all the data records as well. If there is no records, however, the csv file won't contain the custom field.

Should work - can't understand why...


I am trying to set up mock data for the test, but it is really a pain cos of all the validation links... In order to add Item, I need to have Supplier -> Supplier Type ...... Is there a way to bypass the validations cos in the end that's not what I wanna test ?

This is the code @@: 

I actually wanna clear the db after finishing testing ... but still very messy trying to find a better way to test...


What are you trying to test, API or PO (?) :)

API is the wiring and you have to assume it works. 

Whats wrong with this system ?


It automates most of the issues

Also the tests you write should not assume a clean slate - there could be previous balances you have to consider in your test cases...

To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/06710966-26c1-47c2-bc03-6135152e4c7b%40googlegroups.com.

Nguyen Do Le Bao

unread,
Aug 15, 2014, 5:01:03 AM8/15/14
to erpnext-dev...@googlegroups.com
Yeah, I have no doubt that the default test for purchase_order works. 
Just that the ones I am testing is customised APIs written by me (aka the RPC calls in this doc: https://frappe.io/developers/api/rest_api).
So far, setting up all the data necessary for the testing is still quite a pain. 

So i am going with another solution, just use Postman Chrome App  to test the APIs (they do have automation tool)... Will get back to this some other time...
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer-forum+unsub...@googlegroups.com.

Nguyen Do Le Bao

unread,
Sep 11, 2014, 11:38:54 PM9/11/14
to erpnext-dev...@googlegroups.com
A slightly different question: How to export an app and its db ? I can push to a git repo the code but how's about the default data ?

Thanks!! 

Rushabh Mehta

unread,
Sep 12, 2014, 1:29:21 AM9/12/14
to erpnext-dev...@googlegroups.com
On 12-Sep-2014, at 9:08 am, Nguyen Do Le Bao <natha...@gmail.com> wrote:

A slightly different question: How to export an app and its db ? I can push to a git repo the code but how's about the default data ?

Thanks!! 

Add to fixtures in hooks.py as "fixtures = ["DocType1", "DocType2"]

and

frappe --export_fixtures app_name

fixtures will be synced / updated with frappe --latest

To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/03198752-51eb-4327-9926-269b55d8e223%40googlegroups.com.

Nguyen Do Le Bao

unread,
Sep 15, 2014, 6:00:53 AM9/15/14
to erpnext-dev...@googlegroups.com
Hey sorry for bother you guys on this ... but I still cannot do this: 

Under frappe-bench/sites, I run: 
frappe --export_fixtures app_name

It return the following error:

Traceback (most recent call last):
  File "/Applications/MAMP/htdocs/source_code/MyProject/new-bench/frappe-bench/env/bin/frappe", line 9, in <module>
    load_entry_point('frappe==4.1.0', 'console_scripts', 'frappe')()
  File "/Applications/MAMP/htdocs/source_code/MyProject/new-bench/frappe-bench/apps/frappe/frappe/cli.py", line 53, in main
    return run(fn, parsed_args)
  File "/Applications/MAMP/htdocs/source_code/MyProject/new-bench/frappe-bench/apps/frappe/frappe/cli.py", line 83, in run
    out = globals().get(fn)(**args)
  File "/Applications/MAMP/htdocs/source_code/MyProject/new-bench/frappe-bench/apps/frappe/frappe/cli.py", line 67, in new_fn
    return fn(*args, **new_kwargs)
  File "/Applications/MAMP/htdocs/source_code/MyProject/new-bench/frappe-bench/apps/frappe/frappe/cli.py", line 669, in export_fixtures
    export_fixtures()
  File "/Applications/MAMP/htdocs/source_code/MyProject/new-bench/frappe-bench/apps/frappe/frappe/utils/fixtures.py", line 23, in export_fixtures
    for app in frappe.get_installed_apps():
  File "/Applications/MAMP/htdocs/source_code/MyProject/new-bench/frappe-bench/apps/frappe/frappe/__init__.py", line 416, in get_installed_apps
    installed = json.loads(db.get_global("installed_apps") or "[]")
  File "/Applications/MAMP/htdocs/source_code/MyProject/new-bench/frappe-bench/apps/frappe/frappe/database.py", line 444, in get_global
    return self.get_default(key, user)
  File "/Applications/MAMP/htdocs/source_code/MyProject/new-bench/frappe-bench/apps/frappe/frappe/database.py", line 457, in get_default
    d = frappe.defaults.get_defaults(parent).get(key)
  File "/Applications/MAMP/htdocs/source_code/MyProject/new-bench/frappe-bench/apps/frappe/frappe/defaults.py", line 52, in get_defaults
    userd = get_defaults_for(user)
  File "/Applications/MAMP/htdocs/source_code/MyProject/new-bench/frappe-bench/apps/frappe/frappe/defaults.py", line 140, in get_defaults_for
    defaults = frappe.cache().get_value("__defaults:" + parent)
  File "/Applications/MAMP/htdocs/source_code/MyProject/new-bench/frappe-bench/apps/frappe/frappe/memc.py", line 19, in get_value
    val = self.get(self.n(key))
  File "/Applications/MAMP/htdocs/source_code/MyProject/new-bench/frappe-bench/apps/frappe/frappe/memc.py", line 10, in n
    return (frappe.conf.db_name + ":" + key.replace(" ", "_")).encode('utf-8')
TypeError: unsupported operand type(s) for +: 'NoneType' and 'unicode'

Which i guess that it cannot find the db_name ? I did set the default site config using bench

Exporting fixtures from app doesn't work, but export from the site totally works:
frappe --export_fixtures site_name
Just that in that case I will need to put everything under frappe/hook.py, which certainly is not ideal. 

Thanks a lot! 
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer-forum+unsub...@googlegroups.com.

Rushabh Mehta

unread,
Sep 15, 2014, 8:52:50 AM9/15/14
to erpnext-dev...@googlegroups.com
Thats the way its supposed to work at the moment!

To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/3df569d1-050d-4e30-9748-c8786e29fd3b%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages