How to set variable in unit test of task
See original GitHub issueI try to set variables before run a task in a unit test
Environment
azure-pipelines-task-lib version: 2.9.3
Issue Description
I tried to set variables in the unit test with
import * as fs from 'fs';
import * as path from 'path';
import { TaskMockRunner } from 'azure-pipelines-task-lib/mock-run';
import { TaskLibAnswers } from 'azure-pipelines-task-lib/mock-answer';
const task: TaskMockRunner = new TaskMockRunner(taskPath);
task.setInput('excelFilePath', excelFilePath);
task.setVariableName('Build.BinariesDirectory', __dirname);
task.setVariableName('Product.Version', '0.1.0');
// Mock answer for excel file path
let answers: TaskLibAnswers = <TaskLibAnswers>{
"checkPath": {
[excelFilePath]: fs.existsSync(excelFilePath)
}
};
task.setAnswers(answers);
task.run();
And in the task i get variables in run function with :
import { TaskResult, VariableInfo, getPathInput, setResult, getVariables } from 'azure-pipelines-task-lib/task';
...
let variables: VariableInfo[] = getVariables();
but the array is always empty
Expected behaviour
The variables array should contains ‘Build.BinariesDirectory’ and ‘Product.Version’
Actual behaviour
The array is always empty I also tried with:
task.setVariableName('BUILD_BINARIESDIRECTORY', __dirname);
task.setVariableName('PRODUCT_VERSION', '0.1.0');
or
process.env['Build.BinariesDirectory'] = __dirname;
process.env['Product.Version'] = '0.1.0';
or
process.env['BUILD_BINARIESDIRECTORY'] = __dirname;
process.env['PRODUCT_VERSION'] = '0.1.0';
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
Set variable name after method execution in Unit test
First you want to assign a value in the callback. == is not assigning a value it is an equality comparison. Second no...
Read more >How to write unitTest for checking variables value?
In userTask I used form and in unitTest I wrote complete(task) and I checked variable for that form I created before. something like...
Read more >Unit testing fundamentals - Visual Studio (Windows)
From the code editor window, right-click and choose Create Unit Tests from the right-click menu.
Read more >Best Practices - Apache Airflow
You can write unit tests for both your tasks and your DAG. ... Make sure your DAG is parameterized to change the variables,...
Read more >Using xUnit to Test your C# Code - Auth0
How to create unit tests and integration tests with xUnit for your C# ... [Fact] public async Task GetGlossaryList() { // Act var...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
ok, I think I understand now. I have a work around for you. The issue appears that when you use a method from task-lib in your unit test, it does some initialization and then the mocking for task.setInput no longer works.
Here is a work around to set the variables and have getVariables() return the right thing:
Yes, thanks for your help