[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sourcing global variables in unit tests

From: Kai Torben Ohlhus
Subject: Re: Sourcing global variables in unit tests
Date: Tue, 16 Apr 2019 12:26:32 +0900

On Tue, Apr 16, 2019 at 1:47 AM Pavel Hofman <address@hidden> wrote:
Dne 15. 04. 19 v 18:29 Pavel Hofman napsal(a):
> Hi,
> Please is there any way to source a script defining global variables in
> unit test (%!test) and have these available in functions called within
> the test?
> This works:
> %!test
> %! global dataDir = '/tmp';
> %! func_using_global_dataDir();
> But this does not work, dataDir is not available in
> func_using_global_dataDir():
> %!test
> %! source 'consts.m';
> %! func_using_global_dataDir();
> Where script consts.m contains
> global dataDir = '/tmp';
> Yet when consts.m is sourced from another script (in the main code, not
> the unit script %! section), the global vars defined in consts.m are
> available in the script as well as all functions called by the script.
> Tested with Octave 4.2.2 in Linux.

It may be related to this problem:

%! global A;
%! A = 1;
%! funcX_calling_funcY();

This global variable A is available in function funcY() called from

But when defining global vars with a one-liner:

%! global A = 1;
%! funcX_calling_funcY();

the global var A is NOT available in funcY(), ONLY in funcX_calling_funcY().

The same holds if the sourced/included file defines the globals with two

global A;
A = 1;

instead of the one-liner

global A = 1;

But the oneliner global works OK in regular code, just unit tests have
this second-level problem.

Again, my Octave is a bit older 4.2.2.

Thanks a lot.


Can you provide a small, comprehensible, and complete test within a ZIP file?   Maybe this already works with Octave 5.1.0.

Otherwise, why don't you call a function "my_consts" on the workspace, providing the necessary test constants?

function [const_a, const_b, ....] = my_consts ()
const_a = pi;

In some test context:

%! [~,b] = my_consts ();
%! ## Some useful test


reply via email to

[Prev in Thread] Current Thread [Next in Thread]