下面列出了org.testng.IClass#io.qameta.allure.model.Link 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@ParameterizedTest
@CsvSource({
"TEST_CASE_ID, tms",
"ISSUE_ID, issue",
"REQUIREMENT_ID, requirement"
})
void shouldCreateLink(VividusLabel vividusLabel, String type)
{
String identifier = "VVD-5";
Optional<Link> optionalLink = vividusLabel.createLink(identifier);
assertTrue(optionalLink.isPresent());
Link link = optionalLink.get();
assertAll(
() -> assertEquals(identifier, link.getName()),
() -> assertEquals(type, link.getType()),
() -> assertEquals(HTTPS + type + "/" + identifier, link.getUrl())
);
}
@AllureFeatures.Links
@ExtendWith(SystemPropertyExtension.class)
@SystemProperty(name = "allure.link.issue.pattern", value = "https://example.org/issue/{}")
@SystemProperty(name = "allure.link.tms.pattern", value = "https://example.org/tms/{}")
@Test
void shouldAddLinks() {
final AllureResults results = runFeature("features/tags.feature");
final List<TestResult> testResults = results.getTestResults();
assertThat(testResults)
.flatExtracting(TestResult::getLinks)
.extracting(Link::getName, Link::getType, Link::getUrl)
.contains(
tuple("OAT-4444", "tms", "https://example.org/tms/OAT-4444"),
tuple("BUG-22400", "issue", "https://example.org/issue/BUG-22400")
);
}
@AllureFeatures.Links
@ExtendWith(SystemPropertyExtension.class)
@SystemProperty(name = "allure.link.issue.pattern", value = "https://example.org/issue/{}")
@SystemProperty(name = "allure.link.tms.pattern", value = "https://example.org/tms/{}")
@Test
void shouldAddLinks() {
final AllureResultsWriterStub writer = new AllureResultsWriterStub();
runFeature(writer, "features/tags.feature");
final List<TestResult> testResults = writer.getTestResults();
assertThat(testResults)
.flatExtracting(TestResult::getLinks)
.extracting(Link::getName, Link::getType, Link::getUrl)
.contains(
tuple("OAT-4444", "tms", "https://example.org/tms/OAT-4444"),
tuple("BUG-22400", "issue", "https://example.org/issue/BUG-22400")
);
}
@AllureFeatures.Links
@ExtendWith(SystemPropertyExtension.class)
@SystemProperty(name = "allure.link.issue.pattern", value = "https://example.org/issue/{}")
@SystemProperty(name = "allure.link.tms.pattern", value = "https://example.org/tms/{}")
@Test
void shouldAddLinks() {
final AllureResults results = runFeature("features/tags.feature");
final List<TestResult> testResults = results.getTestResults();
assertThat(testResults)
.flatExtracting(TestResult::getLinks)
.extracting(Link::getName, Link::getType, Link::getUrl)
.contains(
tuple("OAT-4444", "tms", "https://example.org/tms/OAT-4444"),
tuple("BUG-22400", "issue", "https://example.org/issue/BUG-22400")
);
}
@AllureFeatures.Links
@ExtendWith(SystemPropertyExtension.class)
@SystemProperty(name = "allure.link.issue.pattern", value = "https://example.org/issue/{}")
@SystemProperty(name = "allure.link.tms.pattern", value = "https://example.org/tms/{}")
@Test
void shouldAddLinks() {
final AllureResultsWriterStub writer = new AllureResultsWriterStub();
runFeature(writer, "features/tags.feature");
final List<TestResult> testResults = writer.getTestResults();
assertThat(testResults)
.flatExtracting(TestResult::getLinks)
.extracting(Link::getName, Link::getType, Link::getUrl)
.contains(
tuple("OAT-4444", "tms", "https://example.org/tms/OAT-4444"),
tuple("BUG-22400", "issue", "https://example.org/issue/BUG-22400")
);
}
@AllureFeatures.Links
@ResourceLock(value = SYSTEM_PROPERTIES, mode = READ_WRITE)
@SystemProperty(name = "allure.link.issue.pattern", value = "https://example.org/issue/{}")
@SystemProperty(name = "allure.link.tms.pattern", value = "https://example.org/tms/{}")
@SystemProperty(name = "allure.link.custom.pattern", value = "https://example.org/custom/{}")
@Test
void shouldProcessMethodLinks() {
final AllureResults results = runClasses(TestWithMethodLinks.class);
final List<TestResult> testResults = results.getTestResults();
assertThat(testResults)
.hasSize(1)
.flatExtracting(TestResult::getLinks)
.extracting(Link::getName, Link::getType, Link::getUrl)
.contains(
tuple("LINK-1", "custom", "https://example.org/custom/LINK-1"),
tuple("LINK-2", "custom", "https://example.org/link/2"),
tuple("", "custom", "https://example.org/some-custom-link"),
tuple("ISSUE-1", "issue", "https://example.org/issue/ISSUE-1"),
tuple("ISSUE-2", "issue", "https://example.org/issue/ISSUE-2"),
tuple("ISSUE-3", "issue", "https://example.org/issue/ISSUE-3"),
tuple("TMS-1", "tms", "https://example.org/tms/TMS-1"),
tuple("TMS-2", "tms", "https://example.org/tms/TMS-2"),
tuple("TMS-3", "tms", "https://example.org/tms/TMS-3")
);
}
@AllureFeatures.Links
@ResourceLock(value = SYSTEM_PROPERTIES, mode = READ_WRITE)
@SystemProperty(name = "allure.link.issue.pattern", value = "https://example.org/issue/{}")
@SystemProperty(name = "allure.link.tms.pattern", value = "https://example.org/tms/{}")
@SystemProperty(name = "allure.link.custom.pattern", value = "https://example.org/custom/{}")
@Test
void shouldProcessClassLinks() {
final AllureResults results = runClasses(TestWithClassLinks.class);
final List<TestResult> testResults = results.getTestResults();
assertThat(testResults)
.hasSize(1)
.flatExtracting(TestResult::getLinks)
.extracting(Link::getName, Link::getType, Link::getUrl)
.contains(
tuple("LINK-1", "custom", "https://example.org/custom/LINK-1"),
tuple("LINK-2", "custom", "https://example.org/link/2"),
tuple("", "custom", "https://example.org/some-custom-link"),
tuple("ISSUE-1", "issue", "https://example.org/issue/ISSUE-1"),
tuple("ISSUE-2", "issue", "https://example.org/issue/ISSUE-2"),
tuple("ISSUE-3", "issue", "https://example.org/issue/ISSUE-3"),
tuple("TMS-1", "tms", "https://example.org/tms/TMS-1"),
tuple("TMS-2", "tms", "https://example.org/tms/TMS-2"),
tuple("TMS-3", "tms", "https://example.org/tms/TMS-3")
);
}
@AllureFeatures.Links
@ExtendWith(SystemPropertyExtension.class)
@SystemProperty(name = "allure.link.issue.pattern", value = "https://example.org/issue/{}")
@SystemProperty(name = "allure.link.tms.pattern", value = "https://example.org/tms/{}")
@Test
void shouldAddLinks() {
final AllureResults results = runFeature("features/tags.feature");
assertThat(results.getTestResults())
.flatExtracting(TestResult::getLinks)
.extracting(Link::getName, Link::getType, Link::getUrl)
.contains(
tuple("OAT-4444", "tms", "https://example.org/tms/OAT-4444"),
tuple("BUG-22400", "issue", "https://example.org/issue/BUG-22400")
);
}
@AllureFeatures.Links
@Test(description = "Tests with links")
public void linksTest() {
final AllureResults results = runTestNgSuites("suites/links.xml");
List<TestResult> testResults = results.getTestResults();
assertThat(testResults)
.hasSize(4)
.filteredOn(hasLinks())
.hasSize(4)
.flatExtracting(TestResult::getLinks)
.extracting(Link::getName)
.contains("testClass", "a", "b", "c", "testClassIssue", "testClassTmsLink",
"testClass", "nested1", "nested2", "nested3", "testClassIssue", "issue1", "issue2", "issue3",
"testClassTmsLink", "tms1", "tms2", "tms3", "testClass", "a", "b", "c", "testClassIssue",
"testClassTmsLink", "testClass", "inheritedLink1", "inheritedLink2", "testClassIssue",
"inheritedIssue", "testClassTmsLink", "inheritedTmsLink"
);
}
@Test
void shouldAddLinks() {
final io.qameta.allure.model.Link first = random(Link.class);
final io.qameta.allure.model.Link second = random(Link.class);
final io.qameta.allure.model.Link third = random(Link.class);
final AllureResults results = runWithinTestContext(
() -> {
link(first.getName(), first.getType(), first.getUrl());
link(second.getName(), second.getUrl());
link(third.getUrl());
},
Allure::setLifecycle
);
assertThat(results.getTestResults())
.flatExtracting(TestResult::getLinks)
.extracting(Link::getName, Link::getType, Link::getUrl)
.contains(
tuple(first.getName(), first.getType(), first.getUrl()),
tuple(second.getName(), null, second.getUrl()),
tuple(null, null, third.getUrl())
);
}
@Override
public void failed(String step, Throwable throwable)
{
super.failed(step, throwable);
checkForBeforeAfterScenarioSteps();
if (!(throwable instanceof UUIDExceptionWrapper) || !(throwable.getCause() instanceof BeforeOrAfterFailed))
{
modifyStepTitle(step);
}
Throwable cause = JBehaveFailureUnwrapper.unwrapCause(throwable);
boolean isVerificationError = cause instanceof VerificationError;
if (isVerificationError)
{
cause = verificationErrorAdapter.adapt((VerificationError) cause);
List<Link> links = new ArrayList<>();
for (KnownIssue knownIssue: ((VerificationError) cause).getKnownIssues())
{
if (!knownIssue.isPotentiallyKnown())
{
links.add(ResultsUtils.createIssueLink(knownIssue.getIdentifier()));
}
}
lifecycle.updateTestCase(getRootStepId(), result -> result.getLinks().addAll(links));
}
stopBddStep(StatusProvider.getStatus(cause), getStatusDetailsFromThrowable(cause));
}
private void stopTestCase()
{
if (allureRunContext.getScenarioExecutionStage() == ScenarioExecutionStage.IN_PROGRESS)
{
stopBddStep(StatusPriority.getLowest().getStatusModel());
}
LinkedQueueItem<String> step = getLinkedStep();
if (!step.isRootItem())
{
stopStep();
return;
}
String id = step.getValue();
String testRunId = getTestRunId();
if (testRunId != null)
{
Link testRunLink = ResultsUtils.createTmsLink(testRunId).setName("Test run ID");
lifecycle.updateTestCase(id, result -> result.getLinks().add(testRunLink));
}
lifecycle.stopTestCase(id);
lifecycle.writeTestCase(id);
allureRunContext.resetScenarioExecutionStage();
switchToParent();
}
@Test
void testAddTestCaseInfo()
{
mockScenarioUid(true);
boolean givenStory = false;
mockStoryStart(givenStory);
Properties scenarioMeta = putTestCaseMetaProperties(getScenarioMeta(false), EXPECTED_SCENARIO_TEST_CASE_ID,
EXPECTED_SCENARIO_REQUIREMENT_ID);
Scenario scenario1 = createScenario(scenarioMeta, List.of());
Scenario scenario2 = createScenario(scenarioMeta, List.of());
RunningScenario runningScenario = getRunningScenario(scenario1, 0);
Story story = new Story(STORY_NAME, null, new Meta(new Properties()), null, List.of(scenario1, scenario2));
RunningStory runningStory = getRunningStory(story, runningScenario);
when(bddRunContext.getRunningStory()).thenReturn(runningStory);
when(bddRunContext.getStoriesChain()).thenReturn(new LinkedList<>(List.of(runningStory)));
allureStoryReporter.beforeStory(story, givenStory);
allureStoryReporter.beforeScenario(story.getScenarios().get(0));
verify(allureLifecycle).scheduleTestCase(testResultCaptor.capture());
TestResult captured = testResultCaptor.getValue();
assertEquals("[batch: my-batch][stories-chain: name][scenario: Scenario-0]", captured.getHistoryId());
assertTestResultLabel(TEST_CASE_ID, EXPECTED_SCENARIO_TEST_CASE_ID);
assertTestResultLabel(REQUIREMENT_ID, EXPECTED_SCENARIO_REQUIREMENT_ID);
List<Link> links = captured.getLinks();
assertFalse(links.isEmpty());
assertEquals(TMS_LINK_PREFIX + EXPECTED_SCENARIO_TEST_CASE_ID, links.get(0).getUrl());
assertEquals(StatusPriority.getLowest().getStatusModel(), captured.getStatus());
}
@Test
void shouldSetLinks() {
final AllureResults results = run(FailedTest.class);
assertThat(results.getTestResults())
.flatExtracting(TestResult::getLinks)
.extracting(Link::getName)
.containsExactlyInAnyOrder("link-1", "link-2", "issue-1", "issue-2", "tms-1", "tms-2");
}
private List<Link> getLinks(final AnnotatedElement annotatedElement) {
return Stream.of(
getAnnotations(annotatedElement, io.qameta.allure.Link.class).map(ResultsUtils::createLink),
getAnnotations(annotatedElement, io.qameta.allure.Issue.class).map(ResultsUtils::createLink),
getAnnotations(annotatedElement, io.qameta.allure.TmsLink.class).map(ResultsUtils::createLink))
.reduce(Stream::concat).orElseGet(Stream::empty).collect(Collectors.toList());
}
private List<Link> extractLinks(final Description description) {
final List<Link> result = new ArrayList<>(getLinks(description.getAnnotations()));
Optional.of(description)
.map(Description::getTestClass)
.map(AnnotationUtils::getLinks)
.ifPresent(result::addAll);
return result;
}
@Test
@AllureFeatures.Links
void shouldSetLinks() {
final AllureResults results = runClasses(FailedTest.class);
List<TestResult> testResults = results.getTestResults();
assertThat(testResults)
.hasSize(1)
.flatExtracting(TestResult::getLinks)
.extracting(Link::getName)
.containsExactlyInAnyOrder("link-1", "link-2", "issue-1", "issue-2", "tms-1", "tms-2");
}
public static <T extends Annotation> List<Link> getLinks(final Method method, final Class<T> annotation,
final Function<T, List<Link>> extractor) {
final List<Link> labels = new ArrayList<>();
labels.addAll(getLinks((AnnotatedElement) method, annotation, extractor));
labels.addAll(getLinks(method.getDeclaringClass(), annotation, extractor));
return labels;
}
private static <T extends Annotation> List<Link> getLinks(final AnnotatedElement element,
final Class<T> annotation,
final Function<T, List<Link>> extractor) {
return element.isAnnotationPresent(annotation)
? extractor.apply(element.getAnnotation(annotation))
: Collections.emptyList();
}
public List<Link> getLinks() {
final Method method = getMethod();
final List<Link> links = new ArrayList<>();
links.addAll(Allure1Utils.getLinks(method, TestCaseId.class, Allure1Utils::createLinks));
links.addAll(Allure1Utils.getLinks(method, Issue.class, Allure1Utils::createLinks));
links.addAll(Allure1Utils.getLinks(method, Issues.class, Allure1Utils::createLinks));
return links;
}
@ParameterizedTest
@MethodSource("testClassesProvider")
void shouldProcessIssuesAnnotation(final SimpleTest simpleTest) {
final AllureResults results = runWithinTestContext(
simpleTest::testSomething,
Allure1TestCaseAspects::setLifecycle,
Allure1ParametersAspects::setLifecycle
);
assertThat(results.getTestResults())
.flatExtracting(TestResult::getLinks)
.extracting(Link::getName)
.containsExactlyInAnyOrder("ISSUE-1", "ISSUE-11", "ISSUE-2", "ISSUE-22", "TEST-1");
}
private List<Link> getLinks(final ITestNGMethod method, final IClass iClass) {
final List<Link> links = new ArrayList<>();
getMethod(method)
.map(AnnotationUtils::getLinks)
.ifPresent(links::addAll);
getClass(iClass)
.map(AnnotationUtils::getLinks)
.ifPresent(links::addAll);
return links;
}
private static Stream<Link> extractLinks(final LinkAnnotation m, final Annotation annotation) {
// this is required as Link annotation uses name attribute as value alias.
if (annotation instanceof io.qameta.allure.Link) {
return Stream.of(createLink((io.qameta.allure.Link) annotation));
}
if (Objects.equals(m.value(), LinkAnnotation.DEFAULT_VALUE)) {
return callValueMethod(annotation)
.map(value -> createLink(value, null, m.url(), m.type()));
}
return Stream.of(createLink(m.value(), null, m.url(), m.type()));
}
@SuppressWarnings("PMD.UseObjectForClearerAPI")
public static Link createLink(final String value, final String name,
final String url, final String type) {
final String resolvedName = firstNonEmpty(value).orElse(name);
final String resolvedUrl = firstNonEmpty(url)
.orElseGet(() -> getLinkUrl(resolvedName, type));
return new Link()
.setName(resolvedName)
.setUrl(resolvedUrl)
.setType(type);
}
private void startTestCase(RunningScenario runningScenario, StoryExecutionStage storyExecutionStage)
{
allureRunContext.setStoryExecutionStage(storyExecutionStage);
if (getLinkedStep() == null)
{
RunningStory runningStory = bddRunContext.getRunningStory();
Meta storyMeta = runningStory.getStory().getMeta();
Meta scenarioMeta = runningScenario.getScenario().getMeta();
Map<VividusLabel, Set<String>> metaLabels = Stream.of(VividusLabel.values())
.collect(Collectors.toMap(Function.identity(),
label -> label.extractMetaValues(storyMeta, scenarioMeta), (l, r) -> l, LinkedHashMap::new));
List<Link> links = metaLabels.entrySet().stream()
.flatMap(e -> e.getValue().stream().map(v -> e.getKey().createLink(v)))
.flatMap(Optional::stream)
.collect(Collectors.toList());
List<Label> labels = metaLabels.entrySet().stream()
.flatMap(e -> e.getValue().stream().map(v -> e.getKey().createLabel(v)))
.collect(Collectors.toList());
labels.addAll(allureRunContext.getCurrentStoryLabels());
int index = runningScenario.getIndex();
String scenarioId = runningScenario.getUuid() + (index != -1 ? "[" + index + "]" : "");
lifecycle.scheduleTestCase(new TestResult()
.setHistoryId(getHistoryId(runningStory, runningScenario))
.setUuid(scenarioId)
.setName(runningScenario.getTitle())
.setLabels(labels)
.setLinks(links)
.setStatus(StatusPriority.getLowest().getStatusModel()));
lifecycle.startTestCase(scenarioId);
putCurrentStepId(scenarioId);
}
else
{
startStep(runningScenario.getTitle());
}
allureRunContext.setStoryExecutionStage(storyExecutionStage);
allureRunContext.setScenarioExecutionStage(ScenarioExecutionStage.BEFORE_STEPS);
}
public List<Link> getScenarioLinks() {
return scenarioLinks;
}
public List<Link> getScenarioLinks() {
return scenarioLinks;
}
public List<Link> getScenarioLinks() {
return scenarioLinks;
}
public static List<Link> createLinks(final Issues issues) {
return Arrays.stream(issues.value())
.map(Allure1Utils::createLink)
.collect(Collectors.toList());
}
public static List<Link> createLinks(final Issue issue) {
return Collections.singletonList(createLink(issue));
}