Clear Filters
Clear Filters

Matlab_xunit , using globals in my test cases

2 views (last 30 days)
Hi All,
I am writing some testcases. Here is a question about what is the right programming practice.
The function i want to write the test case for is using a couple of globals (and there will be some which are using 5-6 globals or more hence I want to do it with right way) but those globals are not being passed to the function since it is globla (duh! ) just used inside the function.
So to write the test case I have to declare the globals in test case as well and I am not sure If that is good practice. If not, what is the right way to do it?
Here is an example I created for you , it is not an actually program
my main program...
function [numbers] = calcAll(text)
global gNum
if strcmp(text, 'Add')
numbers = gNum + 3;
else
numbers = gNum * 3;
end
end
Now my test case would be something like this
function [] = testingCalcAll()
global gNum = load('global.mat'); % Saved global.mat for testing
goldNumbers = load('output.mat'); % i have output of calcAll() saved here
testNumbers = calcAll('Add');
if ~isequal(goldNumbers, testNumbers)
TestCaseStatus = 'FAIL'
else
TestCaseStatus = 'PASS'
end
end
So again the question is, is it all right to have global like gNum in my testingCalcAll()? Or is there a better way to do it?
Thanks

Accepted Answer

Image Analyst
Image Analyst on 14 Feb 2015
I never assign the global on the same line as I declare it.

More Answers (0)

Categories

Find more on Testing Frameworks in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!