Unit tests

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Unit tests

phofman
Hi,

Thanks for the very useful unit test feature of octave.

Is it possible to test an internal function of a script? E.g. the main
function (named after the script) does something complex which is hard
to test (storing/loading file) and the actual features to test are
implemented in internal functions within the script. I have not found I
way to run the internal function in the %!test section.


Is it possible to run all tests within a directory/subdirectories, IOW
testing the whole project? I have tried:

test ./

test *m

test ../MYDIR/



Thanks a lot for any hints.

Best regards,

Pavel.



Reply | Threaded
Open this post in threaded view
|

Re: Unit tests

mmuetzel
On 18 Feb 2019 07:53:35 Pavel Hofman wrote:
> Is it possible to test an internal function of a script? E.g. the main
> function (named after the script) does something complex which is hard
> to test (storing/loading file) and the actual features to test are
> implemented in internal functions within the script. I have not found I
> way to run the internal function in the %!test section.

Unfortunately, it is not possible to run tests for local or private functions.
Only tests for functions that are in the global scope can be run. This are functions in the path that can be called directly from e.g. the command window.

> Is it possible to run all tests within a directory/subdirectories, IOW
> testing the whole project?

Try the following:
runtests path_to_files_with_tests


Markus


Reply | Threaded
Open this post in threaded view
|

Re: Unit tests

phofman
Hi Markus,

Thanks a lot for your valuable info.

Dne 18. 02. 19 v 15:07 "Markus M├╝tzel" napsal(a):
> On 18 Feb 2019 07:53:35 Pavel Hofman wrote:
>> Is it possible to test an internal function of a script? E.g. the main
>> function (named after the script) does something complex which is hard
>> to test (storing/loading file) and the actual features to test are
>> implemented in internal functions within the script. I have not found I
>> way to run the internal function in the %!test section.
>
> Unfortunately, it is not possible to run tests for local or private functions.
> Only tests for functions that are in the global scope can be run. This are functions in the path that can be called directly from e.g. the command window.

OK, I will copy the private functions into separate files for unit testing.
>
>> Is it possible to run all tests within a directory/subdirectories, IOW
>> testing the whole project?
>
> Try the following:
> runtests path_to_files_with_tests

Exactly what I was looking for, works great. Thanks!

Pavel.


Reply | Threaded
Open this post in threaded view
|

Re: Unit tests

apjanke-floss
In reply to this post by phofman
Hi, Pavel,

On 2/18/19 1:53 AM, Pavel Hofman wrote:
> Hi,
>
> Thanks for the very useful unit test feature of octave.
>
> Is it possible to test an internal function of a script? E.g. the main
> function (named after the script) does something complex which is hard
> to test (storing/loading file) and the actual features to test are
> implemented in internal functions within the script. I have not found
> I way to run the internal function in the %!test section.
I don't think this is possible, because test code is executed in a
global context; but local functions (which is what I think you're
referring to here by "internal functions") are only visible from the
context of other functions in that file; that is, in a stack frame
workspace that is executing one of those functions.

The way I get around this is by sticking my "for internal use"
implementation functions in a +myproject/+internal namespace. That way
you can reference them from a global context (including test code), but
it's clear that they're not part of the project's public API. And to
avoid cluttering up that namespace with a bunch of .m files, I'll often
make them static methods on a class in a single classdef .m file.

> Is it possible to run all tests within a directory/subdirectories, IOW
> testing the whole project? I have tried:
>
> test ./
>
I think you want runtests() instead of test() here. test() tests a
single function or class; runtests() runs a suite of tests from one or
more directories. There's also __run_test_suite__, which will execute
Octave's own internal tests.

Cheers,
Andrew


Reply | Threaded
Open this post in threaded view
|

Re: Unit tests

phofman
Hi Andrew,

Dne 05. 03. 19 v 3:25 Andrew Janke napsal(a):
>
> The way I get around this is by sticking my "for internal use"
> implementation functions in a +myproject/+internal namespace. That way
> you can reference them from a global context (including test code), but
> it's clear that they're not part of the project's public API. And to
> avoid cluttering up that namespace with a bunch of .m files, I'll often
> make them static methods on a class in a single classdef .m file.

Thanks for the clever hints! I will use the internal dir method.

As of classes - I read that octave GUI does not support breakpoints in
class methods. Perhaps the new version has it already implemented, not
tested yet. I need breakpoints in GUI for development, being spoilt by
advanced IDEs for regular languages :-)

> I think you want runtests() instead of test() here. test() tests a
> single function or class; runtests() runs a suite of tests from one or
> more directories.

Yes, runtests works great, thanks.

With regards,

Pavel.


Reply | Threaded
Open this post in threaded view
|

Re: Unit tests

apjanke-floss


On 3/5/19 2:46 AM, Pavel Hofman wrote:

> As of classes - I read that octave GUI does not support breakpoints in
> class methods. Perhaps the new version has it already implemented, not
> tested yet. I need breakpoints in GUI for development, being spoilt by
> advanced IDEs for regular languages :-)

I hear ya. No, breakpoints in class methods are still unimplemented as
far as I know. I still debug them by manually inserting "keyboard"
statements. So stick to lots of function .m files for now. :)

>> I think you want runtests() instead of test() here. test() tests a
>> single function or class; runtests() runs a suite of tests from one or
>> more directories.
>
> Yes, runtests works great, thanks.


You may also be interested in my own Testify for Octave project
(https://github.com/apjanke/octave-testify). It's an enhanced unit test
(BIST) interface that provides nicer summary info and stuff when running
tests on directories containing multiple source files. If there's demand
for it, I'll see about adding an xUnit framework port.

Cheers,
Andrew