On Thu, Feb 27, 2020 at 8:17 PM Xu Wang <[email protected]> wrote:
I am writing a script in Leo, it is located in the body of a @button node.
>
> As adding more and more logic to the script, I feel I need to have some
> test case to make me confident on the development progress.
>
[snip]
I created a test_main() and put the test logic there. Using ctrl-b to see
> the result of test_main().
>
>
> It's not a good way:
>
> 1) The test cases and production code are mixed
>
> 2) Need manually disable/enable for different test scenarios.
>
>
> I'm thinking to use python unittest library, but the way I use unittest
> before was in command line, create a dedicated "test_button_body_code.py"
> and "import button_body_code" to test button_body_code.py.
>
>
> How shall I do the similar thing for leo @button script?
>
> Or any other best practice for testing the script in the body of @button?
>
It's a good question. Here are some ideas.
*1. Do everything in the @button node, and its children.*
Complex @button node probably are best written using a controller class.
The pattern in the top-level @button node would be something like:
<< imports >>
class Controller:
@others
Controller(c).run()
You could extend this pattern by using a Test class, a subclass of
unittest.TestCase. A single node can't use more than one @others
directive, so the top-level pattern would become:
<< imports >>
@others # define all classes
Controller(c).run()
The @button script could the run the Test class using functions in the
unittest class.
*2. Use a helper .py file*
Your script could import the file, say x.py. Something like this.
<< imports >>
from x import Controller
Controller(c).run()
Here is the @test button I used to test leoAst.py:
g.cls()
import os
leo_editor_dir = os.path.join(g.app.loadDir, '..', '..')
os.chdir(leo_editor_dir) # must be done here.
file_ = 'leo.core.leoAst'
if 1: # Run all tests
commands = f"&python -m unittest {file_}"
g.execute_shell_commands(commands, trace=False)
else:
file_ = 'leo.core.leoAst'
class_, test = '.TestOrange', '.test_blank_lines_after_function'
commands = f"&python -m unittest {file_}{class_}{test}"
g.execute_shell_commands(commands, trace=False)
This works very well. Furthermore, if x.py ends with:
if __name__ == '__main__':
unittest.main()
everything will be set up to run pytest coverage tests.
I used the following .bat file to run coverage tests:
pytest --cov-report html --cov-report term-missing --cov leo.core.leoAst leo
/core/leoAst.py
HTH.
Edward
--
You received this message because you are subscribed to the Google Groups
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/leo-editor/60f6e1c5-1023-4ae2-89e0-9c309baeea8d%40googlegroups.com.