Class client to execute CRUD operations wit the next Azure Devops WorkItems: TestCases, TesRuns and TestReults
npm install bhd-azuredevops-clientAzureDevOpsClient class allows executing CRUD operations with the following Azure DevOps elements:
bash
npm install dotenv
`
$3
Create a .env file in the project root with the following variables:
`env
AZURE_DEVOPS_BASE_URL=https://dev.azure.com/your-organization/your-project
AZURE_DEVOPS_PAT=your-personal-access-token
TEST_PLAN_ID=12345
`
Usage
$3
`javascript
const { AzureDevOpsClient } = require('./apis/azureDevOpsClient.js');
`
$3
`javascript
// Create a client instance with the test suite ID
const testSuiteId = "67890";
const azureClient = new AzureDevOpsClient(testSuiteId);
`
$3
#### 1. Register a Complete Test Run
`javascript
// Test information
const testInfo = {
title: "Automation-12345-Login functionality test",
status: "PASSED" // or "FAILED"
};
// Register the test run
try {
await azureClient.registerTestRun(testInfo);
console.log("Test run registered successfully");
} catch (error) {
console.error("Error registering test run:", error.message);
}
`
#### 2. Get Test Points
`javascript
try {
const response = await azureClient.getTestPoints();
await azureClient.verifyStatusResponseCode(response, 200, 'GetTestPoints');
const jsonResponse = await azureClient.getResponseAsJson(response);
console.log("Test Points:", jsonResponse.value);
} catch (error) {
console.error("Error getting test points:", error.message);
}
`
#### 3. Create a Test Run Manually
`javascript
const testRunData = {
type: "Normal",
name: "My Test Run",
comment: "Automated test",
plan: {
id: process.env.TEST_PLAN_ID
},
pointIds: [123456] // Test point IDs
};
try {
const response = await azureClient.createTestRun(testRunData);
await azureClient.verifyStatusResponseCode(response, 200, 'CreateTestRun');
const result = await azureClient.getResponseAsJson(response);
console.log("Test Run created:", result.id);
} catch (error) {
console.error("Error creating test run:", error.message);
}
`
#### 4. Update Test Result
`javascript
const testResultData = [{
id: 100000,
outcome: "Passed", // or "Failed"
comment: "Test result is satisfactory"
}];
const testRunId = "987654";
try {
const response = await azureClient.updateTestResult(testResultData, testRunId);
await azureClient.verifyStatusResponseCode(response, 200, 'UpdateTestResult');
console.log("Test result updated");
} catch (error) {
console.error("Error updating result:", error.message);
}
`
#### 5. Search Test Point by Test Case ID
`javascript
const testCaseId = "12345";
try {
const testPointId = await azureClient.getTestPointByTestCaseId(testCaseId);
if (testPointId) {
console.log(Test Point found: ${testPointId});
} else {
console.log("Test Point not found");
}
} catch (error) {
console.error("Error searching test point:", error.message);
}
`
#### 6. Create Test Result Attachment
`javascript
const attachmentData = {
fileName: "screenshot.png",
comment: "Test execution screenshot",
attachmentType: "GeneralAttachment"
};
const testRunId = "987654";
const testResultId = "100000"; // Optional, defaults to '100000'
try {
const response = await azureClient.createTestResultAttachment(
attachmentData,
testRunId,
testResultId
);
await azureClient.verifyStatusResponseCode(response, 200, 'CreateAttachment');
console.log("Attachment created successfully");
} catch (error) {
console.error("Error creating attachment:", error.message);
}
`
Test Case Title Format
The test case title must follow this format:
`
[Prefix]-[Numeric_ID]-[Test_Title]
`
Example:
`
Automation-12345-Login functionality test
`
Where:
- Automation: Identifier prefix
- 12345: Numeric ID of the test case in Azure DevOps
- Login functionality test: Test description
Available Methods
$3
| Method | Description | Parameters |
|--------|-------------|------------|
| registerTestRun(testInfo) | Registers a complete test run | testInfo: object with title and status |
| getTestPoints() | Gets all test points from the suite | None |
| createTestRun(data) | Creates a new test run | data: object with test run data |
| updateTestResult(data, testRunId) | Updates test result | data: result data, testRunId: run ID |
| createTestResultAttachment(data, testRunId, testResultId) | Creates attachment for test result | data: attachment data, testRunId: run ID, testResultId: result ID (optional) |
| getTestPointByTestCaseId(testCaseId) | Searches test point by case ID | testCaseId: test case ID |
$3
| Method | Description | Parameters |
|--------|-------------|------------|
| verifyStatusResponseCode(response, expectedCode, endpointName) | Verifies HTTP status code | response: HTTP response, expectedCode: expected code, endpointName: endpoint name |
| getResponseAsJson(response) | Converts response to JSON | response: HTTP response |
| getResponseAsText(response) | Converts response to text | response: HTTP response |
| verifyTestTitleFormat(title) | Verifies title format | title: title to verify |
| toBase64(str) | Converts string to Base64 | str: input string |
Error Handling
The class uses TypeError for validation errors. It's recommended to use try-catch blocks:
`javascript
try {
await azureClient.registerTestRun(testInfo);
} catch (error) {
if (error instanceof TypeError) {
console.error("Validation error:", error.message);
} else {
console.error("Unexpected error:", error);
}
}
`
Project Structure
`
├── apis/
│ ├── azureDevOpsClient.js
│ ├── package.json
│ └── azureDevOps/
│ ├── testPoints/
│ ├── testResultAttachments/
│ ├── testResults/
│ └── testRuns/
├── .env
└── README.md
`
Azure DevOps Configuration
$3
1. Go to Azure DevOps → User Settings → Personal Access Tokens
2. Create a new token with the following permissions:
- Test Management: Read & Write
- Work Items: Read
$3
- Test Plan ID: Available in the test plan URL
- Test Suite ID: Available in the test suite URL
- Organization/Project: Part of the Azure DevOps base URL
API Response Examples
$3
`json
{
"value": [
{
"id": 1,
"testCase": {
"id": "12345",
"name": "Login Test"
},
"configuration": {
"id": "1",
"name": "Windows 10"
}
}
]
}
`
$3
`json
{
"id": 987654,
"name": "My Test Run",
"state": "InProgress",
"isAutomated": false,
"plan": {
"id": "12345"
}
}
`
Best Practices
1. Environment Variables: Always use environment variables for sensitive data like PATs
2. Error Handling: Implement proper error handling for all async operations
3. Logging: Use console logging for debugging and monitoring
4. Validation: Validate test case title format before processing
5. Status Codes: Always verify HTTP response status codes
Common Issues and Solutions
$3
Solution: Ensure TEST_PLAN_ID is set in your .env file
$3
Solution: Verify the test case title follows the format: Prefix-NumericID-Title
$3
Solution: Ensure the test case ID exists in the specified test suite
$3
Solution: Verify your PAT has the correct permissions and is properly encoded
Contributing
To contribute to the project:
1. Fork the repository
2. Create a feature branch (git checkout -b feature/new-functionality)
3. Commit your changes (git commit -am 'Add new functionality')
4. Push to the branch (git push origin feature/new-functionality)
5. Create a Pull Request
License
This project is licensed under the ISC License.
Author
Hector S. Medina Acosta
---
$3
- Ensure environment variables are properly configured before using the class
- Test case IDs must exist in Azure DevOps
- Personal Access Token must have the necessary permissions
- All methods are asynchronous and should be used with await or .then()`