Using reload has made a big difference in my unit testing. I can run tests 
repeatedly in unitTest.leo, even after I have changed (in leoPy.leo) the 
code being tested.

This workflow about doubles my testing productivity.  True test-driven 
development is possible, here and now, in Leo.

The guidelines are simple:

1. @test nodes should use imp.reload(x) to reload all modules x affected by 
the tests.

2. @test nodes must create new instances of all classes being tested.

When creating a new @test node, cutting and pasting code from previous unit 
tests usually suffices. Fancy @button scripts that create preamble code are 
not required, but might be useful if you are creating lots of tests at once.

The rest of the post will explain these guidelines in more detail, using 
this example...

*Example*

Here is the beginning of @test elisp importer:

if 1: # Enable TDD
    # The preamble...
    g.cls()
    if c.isChanged(): c.save()
    # import leo
    import leo.core.leoImport as leoImport
    import leo.plugins.importers.linescanner as linescanner
    import leo.plugins.importers.elisp
    # Reload all.
    import imp
    imp.reload(leo.plugins.importers.linescanner)
    imp.reload(leo.plugins.importers.elisp)
    imp.reload(leoImport)
    # instantiate the class
    ic = leoImport.LeoImportCommands(c)
else: # Run the test in "production mode".
    ic = c.importCommands
# run the test.
ic.elispUnitTest(p,s=s,showTree=True)

*Using imp reload*

As you can see, the test imports and reloads three modules. The test uses 
leoImport, so it too must be reloaded even thought leoImport.py doesn't 
change.

*Instantiating classes*

imp.reload is not enough. imp.reload doesn't change existing objects. You 
must create (instantiate) new objects to test.  This part of the example 
shows how:

if 1: # Enable TDD
    # The preamble, not shown...
    # instantiate the class
    ic = leoImport.LeoImportCommands(c)
else: # Run the test in "production mode".
    ic = c.importCommands
# run the test.
ic.elispUnitTest(p,s=s,showTree=True)

And that's all there is to it.  Creating the preamble as in the example is 
an investment that quickly pays off.  Give this kind of code a try.  You'll 
be glad you did.

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 post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.

Reply via email to