I want to use junit
for my test case. I have configured junit
for my test. Please go through the code
import http from "k6/http";
import { check, sleep, group } from "k6";
import { Trend } from "k6/metrics";
import { jUnit } from 'https://jslib.k6.io/k6-summary/0.0.1/index.js';
import {
options,
login,
generateRandomSleepTimer,
generateRandomPartnerProfileID,
} from "./configs/config.js";
import { BASE_URL } from "../test/support/constants/endpoints.js";
import {
generateSamplePartnerDataFinal,
generateRandomPhoneNumber,
generateRandomOrganisationContactID,
generateRandomPartnerTypeId,
} from "../test/support/data/perf.sampledata.js";
const jUnitReport = new jUnit();
let TC01_FetchOrderDetails = new Trend("TC01_FetchOrderDetails");
let TC02_FetchListOfOrganisations = new Trend("TC02_FetchListOfOrganisations");
let TC03_FetchOrganisationContact = new Trend("TC03_FetchOrganisationContact");
let partner_profile_id;
export function fetchOrderDetails(authToken, sleepTime) {
group("TC01 - Fetch Order Details", function () {
const response = http.get(`${BASE_URL}/order?leadId=278`, {
headers: {
Authorization: authToken,
},
tags: { name: "Fetch Order Details" },
});
// Measure response duration
const responseDuration = response.timings.duration;
TC01_FetchOrderDetails.add(responseDuration);
// Check if status is 200
const isStatus200 = check(response, {
"is status 200": (r) => r.status === 200,
});
// Parse JSON response and validate message
let isSuccess = false;
let responseMessage = null;
try {
responseMessage = response.json();
isSuccess = check(responseMessage, {
"Success!": (r) => r.message === "Success!",
});
} catch (e) {
console.error("Failed to parse JSON response:", response.body);
}
// Log results
console.log(
`TC01 - Fetch Order Details: ${
isStatus200 && isSuccess ? "PASSED" : "FAILED"
} - Response Time: ${responseDuration} ms`
);
// Add results to JUnit report
jUnitReport.add({
name: "TC01 - Fetch Order Details",
success: isStatus200 && isSuccess,
time: responseDuration,
});
// Sleep for a random duration
sleep(sleepTime);
});
}
export function fetchListOfOrganisations(authToken, sleepTime) {
group("TC02 - Retrieve a list of organisations", function () {
const response = http.get(`${BASE_URL}/organisation/organisations`, {
headers: {
Authorization: authToken,
},
tags: { name: "Retrieve a list of organisations" },
});
const responseDuration = response.timings.duration;
TC02_FetchListOfOrganisations.add(responseDuration);
const checkResult = check(response, {
"is status 200": (r) => r.status === 200,
});
let responseMessage;
try {
responseMessage = response.json();
} catch (e) {
console.error("Failed to parse JSON response: ", response.body);
}
check(responseMessage, {
"Success!": (r) => r && r.message === "Success!",
});
console.log(
`TC02 - Retrieve a list of organisations: ${
checkResult ? "PASSED" : "FAILED"
} ${
responseMessage && responseMessage.message === "Success!"
? "PASSED"
: "FAILED"
} - Response Time: ${responseDuration} ms`
);
jUnitReport.add({
name: "TC02 - Retrieve a list of organisations",
success:
checkResult &&
responseMessage &&
responseMessage.message === "Success!",
time: responseDuration,
});
sleep(sleepTime);
});
}
export function fetchListOfOrganisationsContact(authToken, sleepTime) {
group("TC03 - Retrieve a list of organisations contacts", function () {
const response = http.get(`${BASE_URL}/organisation/organisationContact`, {
headers: {
Authorization: authToken,
},
tags: { name: "Retrieve a list of organisations contacts" },
});
const responseDuration = response.timings.duration;
TC03_FetchOrganisationContact.add(responseDuration);
const checkResult = check(response, {
"is status 200": (r) => r.status === 200,
});
let responseMessage;
try {
responseMessage = response.json();
} catch (e) {
console.error("Failed to parse JSON response: ", response.body);
}
check(responseMessage, {
"Success!": (r) => r && r.message === "Success!",
});
console.log(
`TC02 - Retrieve a list of organisations: ${
checkResult ? "PASSED" : "FAILED"
} ${
responseMessage && responseMessage.message === "Success!"
? "PASSED"
: "FAILED"
} - Response Time: ${responseDuration} ms`
);
jUnitReport.add({
name: "TC03 - Retrieve a list of organisations contacts",
success:
checkResult &&
responseMessage &&
responseMessage.message === "Success!",
time: responseDuration,
});
sleep(sleepTime);
});
}
export default function () {
const authToken = login();
if (!authToken) {
console.error("No auth token, exiting test");
return;
}
const sleepTime = generateRandomSleepTimer();
const partnerProfileID = generateRandomPartnerProfileID();
// Variable for global use
let generateSamplePartnerData = generateSamplePartnerDataFinal();
fetchOrderDetails(authToken, sleepTime);
fetchListOfOrganisations(authToken, sleepTime);
fetchListOfOrganisationsContact(authToken, sleepTime);
// Save JUnit report
jUnitReport.save("results.xml");
}
when I run the test with k6 run .\test\b2b.perftest.js
I can see the result but when I trying with
k6 run --out junit=result.xml .\test\b2b.perftest.js
getting error
ERRO[0002] TypeError: Cannot read property 'metrics' of undefined