Recent

Author Topic: Testable code?  (Read 1103 times)

karlen

  • Newbie
  • Posts: 5
Testable code?
« on: August 09, 2020, 06:06:56 pm »
How to write testable code for Lazarus GUI app? What are the most important things to get into account?

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Testable code?
« Reply #1 on: August 09, 2020, 06:12:01 pm »
Please explain further. because this is madness to give an answer to
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Testable code?
« Reply #2 on: August 09, 2020, 06:33:09 pm »
@karlen

Did you mean unit testing?
https://en.wikipedia.org/wiki/Unit_testing

Maybe you will be interested with fpcunit:
https://wiki.freepascal.org/fpcunit

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Testable code?
« Reply #3 on: August 09, 2020, 06:47:05 pm »
To be able to write good unit tests, you of course need to write testable code, which requires abstraction from the environment and the GUI.

Basically your logic, which is unit testable should be independent of the GUI code, because testing GUIs is really hard.

karlen

  • Newbie
  • Posts: 5
Re: Testable code?
« Reply #4 on: August 09, 2020, 07:16:32 pm »
Please explain further. because this is madness to give an answer to
Thank you for answers and links. I mean primarily unit testing. How to divide code into Form units, Data Modules and others?
Where to include event handlers? I hope from the outset get a sensible architecture, that make it possible to do testing.
How to make sure, that GUI code and logic are separated? I am trying to develop a double entry bookkeeping app for
small enterprises (using SQLite) on Windows.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Testable code?
« Reply #5 on: August 09, 2020, 07:56:35 pm »
Please explain further. because this is madness to give an answer to
Thank you for answers and links. I mean primarily unit testing. How to divide code into Form units, Data Modules and others?
Where to include event handlers? I hope from the outset get a sensible architecture, that make it possible to do testing.
How to make sure, that GUI code and logic are separated? I am trying to develop a double entry bookkeeping app for
small enterprises (using SQLite) on Windows.
One methodology would be to develop a bookkeeping engine with an input unit, a calculation-and-bookkeeping unit and a reporting unit, with a datamodule housing your databases, SQL queries etc with appropriate input and output APIs.
Then develop a small console testing app which uses readlns (or file reads) for input and writelns for output to stress test your engine for all likely and unlikely values.

When you have shaped your engine and probably re-designed it several times until it does what you want, you then debug it so the basic CRUD and bookkeeping operations work flawlessly,

Then design your user interface. Doing this last ensures it is properly decoupled from the data manipulation engine.
If it is a simple app, you might get away with using TDatasources, DBEdits and so on. But that is less than ideal.

You can use a ready-made ORM (there are several mature Pascal alternatives, but a learning curve of course if you have not used one before); or for a simple app you can link UI data input to your engine fairly easily using the standard edit, combo and spin edit components.

karlen

  • Newbie
  • Posts: 5
Re: Testable code?
« Reply #6 on: August 09, 2020, 08:37:58 pm »
Please explain further. because this is madness to give an answer to
Thank you for answers and links. I mean primarily unit testing. How to divide code into Form units, Data Modules and others?
Where to include event handlers? I hope from the outset get a sensible architecture, that make it possible to do testing.
How to make sure, that GUI code and logic are separated? I am trying to develop a double entry bookkeeping app for
small enterprises (using SQLite) on Windows.
One methodology would be to develop a bookkeeping engine with an input unit, a calculation-and-bookkeeping unit and a reporting unit, with a datamodule housing your databases, SQL queries etc with appropriate input and output APIs.
Then develop a small console testing app which uses readlns (or file reads) for input and writelns for output to stress test your engine for all likely and unlikely values.

When you have shaped your engine and probably re-designed it several times until it does what you want, you then debug it so the basic CRUD and bookkeeping operations work flawlessly,

Then design your user interface. Doing this last ensures it is properly decoupled from the data manipulation engine.
If it is a simple app, you might get away with using TDatasources, DBEdits and so on. But that is less than ideal.

You can use a ready-made ORM (there are several mature Pascal alternatives, but a learning curve of course if you have not used one before); or for a simple app you can link UI data input to your engine fairly easily using the standard edit, combo and spin edit components.
Thank you very much for your friendly and detailed answer. Now I have a lot to do...

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Testable code?
« Reply #7 on: August 09, 2020, 08:49:25 pm »
Perhaps I should have added that the complexity of even a small enterprise double entry system should not be underestimated.
You actually need to start by nailing down the requirements.
Can you ignore VAT and sales tax, or is this needed from the beginning? Will you track sales, customers, and purchases, suppliers separately? Are you tracking stock and integrating that with balance sheet and net worth? Do you need international currency exchange? Do you need internet access to bank accounts? Do you need point of sale functionality... ability to do online sales etc. etc.

karlen

  • Newbie
  • Posts: 5
Re: Testable code?
« Reply #8 on: August 09, 2020, 09:43:31 pm »
Perhaps I should have added that the complexity of even a small enterprise double entry system should not be underestimated.
You actually need to start by nailing down the requirements.
Can you ignore VAT and sales tax, or is this needed from the beginning? Will you track sales, customers, and purchases, suppliers separately? Are you tracking stock and integrating that with balance sheet and net worth? Do you need international currency exchange? Do you need internet access to bank accounts? Do you need point of sale functionality... ability to do online sales etc. etc.
OK! I know what I have ahead. I have worked with bookkeeping over 20 years in own accounting enterprise in Finland, so I am familiar with problems of the line, VAT and like.
I have already done the requirements spec for branch items. The app is designed for enterprises that have max. ~1000 journal entries per year, and also for bookkeeping
offices with max. ~100 customers. VAT is a must, international currency not, no other sales, supplier, stock tracking, and any other actions.
SQLite could be a challenge, but there should be option for other databases.

What is missing, is experience specifically with Lazarus (and English language skills!!). The only book about Lazarus I have is your 'Learn to program using Lazarus' and I am using it as base.
Thank you again for your interest in my question!

 

TinyPortal © 2005-2018