|
From: | John W. Eaton |
Subject: | Re: Re: Sourcing global variables in unit tests |
Date: | Tue, 16 Apr 2019 09:51:50 -0400 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.5.1 |
On 4/16/19 8:10 AM, Pavel Hofman wrote:
Hi Kai, Dne 16. 04. 19 v 5:26 Kai Torben Ohlhus napsal(a):Can you provide a small, comprehensible, and complete test within a ZIP file? Maybe this already works with Octave 5.1.0.This very simple example - first test works, the second one fails. function [A, B] = f1Direct() global A; global B; endfunction %!test %! global A; %! A = 2; %! global B; %! B = 3; %! [o1, o2] = f1Direct(); %! expected = [2, 3]; %! assert([o1, o2], expected); %!test %! global A = 1; %! global B = 2; %! [o1, o2] = f1Direct(); %! expected = [1, 2]; %! assert([o1, o2], expected); >> test f1Direct.m ***** test global A = 1; global B = 2; [o1, o2] = f1Direct(); expected = [1, 2]; assert([o1, o2], expected); !!!!! test failed ASSERT errors for: assert ([o1, o2],expected) Location | Observed | Expected | Reason (1) 2 1 Abs err 1 exceeds tol 0 (2) 3 2 Abs err 1 exceeds tol 0
Initialization of global variables only happens once. You can make your tests work if you clear the global values at the end of each test:
%!test %! global A = 2; %! global B = 3; %! [o1, o2] = f1Direct(); %! expected = [2, 3]; %! assert([o1, o2], expected); %! clear -global A B %!test %! global A = 1; %! global B = 2; %! [o1, o2] = f1Direct(); %! expected = [1, 2]; %! assert([o1, o2], expected); %! clear -global A BI think this is better anyway, as it makes each test stand alone and avoids leaking global values into your environment after the tests are run.
It should be possible to initialize and clear the global values in a separate script or function if needed.
jwe
[Prev in Thread] | Current Thread | [Next in Thread] |