Guest User

Untitled

a guest
Jan 23rd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. // sample Execute Command for executing .NET core test with node executor
  2.  
  3. const fs = require('fs');
  4. const path = require('path');
  5. const { execSync } = require('child_process');
  6.  
  7. let isWin = process.platform == "win32";
  8.  
  9. // process.env.WORKING_DIR holds the value of Working Directory configured in Universal Agent,
  10. // if you did not specify Working Directory in Universal Agent, make sure you enter it here
  11. let SOLUTION_DIR = process.env.WORKING_DIR || ``;
  12. // validate the existence of SOLUTION_DIR, it it does not exist, print error and stop execution
  13. if (!fs.existsSync(SOLUTION_DIR)) {
  14. console.error('No working directory found.');
  15. return;
  16. }
  17.  
  18. // change .NET core version to fit your need
  19. let TARGET_DOTNETCORE_VERSION = '2.2';
  20.  
  21. // path to test project, change it to reflect yours
  22. let TEST_PROJECT_NAME = 'NUnitTestSample';
  23. let TEST_PROJECT_DIR = path.resolve (SOLUTION_DIR, 'DotnetCore', TEST_PROJECT_NAME);
  24. let TEST_PROJECT_PATH = path.resolve(TEST_PROJECT_DIR, `${TEST_PROJECT_NAME}.csproj`);
  25.  
  26. // possible value for configuration: Debug or Release
  27. let CONFIGURATION = 'Debug';
  28. // we are going to execute published test
  29. let PUBLISH_DIR = path.resolve(TEST_PROJECT_DIR, 'bin', CONFIGURATION, `netcoreapp${TARGET_DOTNETCORE_VERSION}`, 'publish');
  30. // path to the test project output
  31. let TEST_PROJECT_PUBLISHED_PATH = path.resolve(PUBLISH_DIR, `${TEST_PROJECT_NAME}.dll`);
  32.  
  33. // this is the full path to dotnet command
  34. // make sure you change it to reflect your environment
  35. let DOTNET_EXECUTABLE_PATH = isWin ? 'C:/Program Files/dotnet/dotnet.exe' : '/usr/local/bin/dotnet';
  36.  
  37. // by default, the result folder will be created at ${SOLUTION_DIR}/TestResults
  38. let RESULT_DIR = path.resolve(`${SOLUTION_DIR}`, 'TestResults');
  39. // this is the path to XML test result
  40. let PATH_TO_XML_RESULT = path.resolve(`${RESULT_DIR}`, 'Results.xml');
  41.  
  42. // delete result dir if it exists
  43. if (fs.existsSync(RESULT_DIR)) {
  44. if (isWin) {
  45. execSync(`rmdir /s /q "${RESULT_DIR}"`);
  46. } else {
  47. execSync(`rm -rf "${RESULT_DIR}"`);
  48. }
  49. }
  50.  
  51. // the shell command builder
  52. var commandBuilder = [];
  53. // set DOTNET_CLI_HOME environment var to working directory for dotnet command to work properly
  54. if (!isWin) {
  55. commandBuilder.push(`export DOTNET_CLI_HOME="${SOLUTION_DIR}"`);
  56. }
  57.  
  58. // execute `dotnet publish` command to publish the test project,
  59. // the published results will be stored in PUBLISH_DIR
  60. commandBuilder.push(`"${DOTNET_EXECUTABLE_PATH}" publish "${TEST_PROJECT_PATH}"`);
  61.  
  62. // copy the chromedriver to the PUBLISH_DIR dir for the Selenium test to run properly
  63. // remove these commands if you're not running Selenium tests and so there will be no chromedriver in the output
  64. let CHROME_DRIVER_NAME = isWin ? 'chromedriver.exe' : 'chromedriver';
  65. let COPY_COMMAND = isWin ? 'copy' : 'cp';
  66. let PATH_TO_CHROME_DRIVER = path.resolve(TEST_PROJECT_DIR, 'bin', CONFIGURATION, `netcoreapp${TARGET_DOTNETCORE_VERSION}`, CHROME_DRIVER_NAME);
  67. commandBuilder.push(`${COPY_COMMAND} "${PATH_TO_CHROME_DRIVER}" "${PUBLISH_DIR}"`);
  68.  
  69. // resolve TESTCASES_AC magic var to get the scheduled test runs
  70. let testcases_AC = $TESTCASES_AC;
  71. testcases_AC = testcases_AC ? testcases_AC.split(',') : [];
  72.  
  73. /**
  74. * Kicks off the test. What it does is to resolve the value of `testcases_AC` variable and validate:
  75. * Case 1: if that variable has value, meaning there is/are test run(s) being scheduled in qTest Manager
  76. * -- executes the the test method whose name matching the automation content
  77. * Case 2: the value of testcases_AC is empty, meaning no test runs being scheduled when the Universal Agent is executed in the first time
  78. * -- executes all the tests within the project output DLL
  79. */
  80. var testMethods = '';
  81. if (testcases_AC && testcases_AC.length > 0) {
  82. testMethods = '/Tests:';
  83. for (let testcase_AC of testcases_AC) {
  84. testMethods += `${testcase_AC},`;
  85. }
  86. testMethods = testMethods.slice(0, -1);// remove the last ','
  87. }
  88.  
  89. if (testMethods != '') {
  90. // run specific test methods, change the log file path if possible
  91. commandBuilder.push(`"${DOTNET_EXECUTABLE_PATH}" vstest "${TEST_PROJECT_PUBLISHED_PATH}" ${testMethods} --logger:"nunit;LogFilePath=${PATH_TO_XML_RESULT}"`);
  92. } else {
  93. // run all tests, change the log file path if possible
  94. commandBuilder.push(`"${DOTNET_EXECUTABLE_PATH}" vstest "${TEST_PROJECT_PUBLISHED_PATH}" --logger:"nunit;LogFilePath=${PATH_TO_XML_RESULT}"`);
  95. }
  96.  
  97. // wrap the command execution in a try catch to make sure the execute command is fully executed
  98. // even when there is error happens.
  99. try {
  100. // build the shell command
  101. let command = isWin ? commandBuilder.join(' && ') : commandBuilder.join('\n');
  102. // execute the shell command
  103. execSync(command, { stdio: "inherit" });
  104. } catch (err) {
  105. console.error(`*** Test execution error ***`);
  106. console.error(err.message || err);
  107. console.error(`*** End test execution error ***`);
  108. }
Add Comment
Please, Sign In to add comment