Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2974: Add overrideGroupsFromCliInParentChildXml test #2975

Merged
merged 4 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Current
Fixed: GITHUB-2974: Command line arguments -groups and -excludegroups override defined groups in a suite xml file (dr29bart)
Fixed: GITHUB-2961: "Unexpected value: 16" error when multiple beforeMethod config methods with firstTimeOnly property run before a test (Krishnan Mahadevan)
Fixed: GITHUB-2904: Add location of docs Github to readme and contributions page (Mohsin Sackeer)
Fixed: GITHUB-2934: Parallel Dataproviders & retries causes test result count to be skewed (Krishnan Mahadevan)
Expand Down
34 changes: 25 additions & 9 deletions testng-core-api/src/main/java/org/testng/xml/XmlSuite.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.testng.xml;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -189,8 +190,6 @@ public String toString() {
public static final Boolean DEFAULT_PRESERVE_ORDER = Boolean.TRUE;
private Boolean m_preserveOrder = DEFAULT_PRESERVE_ORDER;

private List<String> m_includedGroups = Lists.newArrayList();
private List<String> m_excludedGroups = Lists.newArrayList();
private XmlMethodSelectors m_xmlMethodSelectors;
private boolean parsed = false;

Expand Down Expand Up @@ -848,23 +847,39 @@ public List<String> getIncludedGroups() {
} else if (m_xmlGroups != null && (m_xmlGroups.getRun() != null)) {
return m_xmlGroups.getRun().getIncludes();
} else {
// deprecated
return m_includedGroups;
// deprecated. Use mutable list because there are unit tests which modifies it
return Lists.newArrayList();
}
}

private void initGroupsRun() {
if (m_xmlGroups == null) {
m_xmlGroups = new XmlGroups();
}
if (m_xmlGroups.getRun() == null) {
m_xmlGroups.setRun(new XmlRun());
}
}

public void addIncludedGroup(String g) {
m_includedGroups.add(g);
initGroupsRun();
m_xmlGroups.getRun().onInclude(g);
}

/** @param g - The list of groups to include. */
public void setIncludedGroups(List<String> g) {
m_includedGroups = g;
initGroupsRun();
List<String> includes = m_xmlGroups.getRun().getIncludes();
includes.clear();
includes.addAll(g);
}

/** @param g The excludedGrousps to set. */
public void setExcludedGroups(List<String> g) {
m_excludedGroups = g;
initGroupsRun();
List<String> excludes = m_xmlGroups.getRun().getExcludes();
excludes.clear();
excludes.addAll(g);
}

/**
Expand All @@ -877,12 +892,13 @@ public List<String> getExcludedGroups() {
} else if (m_xmlGroups != null && (m_xmlGroups.getRun() != null)) {
return m_xmlGroups.getRun().getExcludes();
} else {
return m_excludedGroups;
return Collections.emptyList();
}
}

public void addExcludedGroup(String g) {
m_excludedGroups.add(g);
initGroupsRun();
m_xmlGroups.getRun().onExclude(g);
}

public Boolean getGroupByInstances() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package test.cli.github2974;

import java.util.ArrayList;
import java.util.List;
import org.testng.Assert;
import org.testng.CommandLineArgs;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.TestNG;
import org.testng.annotations.Test;
import test.SimpleBaseTest;

public class OverrideGroupsCliTest extends SimpleBaseTest {

private static class NameCollector implements ITestListener {
List<String> names = new ArrayList<>();

@Override
public void onTestSuccess(ITestResult result) {
names.add(result.getName());
}
}

@Test(description = "GITHUB-2974")
public void overrideIncludeGroupsFromCliInParentChildXml() {
TestNG testNG =
new TestNG(false) {
{
CommandLineArgs args = new CommandLineArgs();
args.groups = "override_group";
args.suiteFiles = List.of(getPathToResource("2974/parent_include.xml"));
configure(args);
}
};
NameCollector collector = new NameCollector();
testNG.addListener(collector);
testNG.run();
Assert.assertTrue(collector.names.contains("overrideTest"));
Assert.assertFalse(collector.names.contains("defaultTest"));
}

@Test(description = "GITHUB-2974")
public void overrideExcludeGroupsFromCliInParentChildXml() {
TestNG testNG =
new TestNG(false) {
{
CommandLineArgs args = new CommandLineArgs();
args.excludedGroups = "override_group";
args.suiteFiles = List.of(getPathToResource("2974/parent_exclude.xml"));
configure(args);
}
};
NameCollector collector = new NameCollector();
testNG.addListener(collector);
testNG.run();
Assert.assertTrue(collector.names.contains("defaultTest"));
Assert.assertFalse(collector.names.contains("overrideTest"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package test.cli.github2974;

import org.testng.annotations.Test;

public class TwoGroupsTest {

@Test(groups = "override_group")
public void overrideTest() {}

@Test(groups = "default_group")
public void defaultTest() {}
}
9 changes: 9 additions & 0 deletions testng-core/src/test/resources/2974/child.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="child">

<test name="Tests inherit groups from parent xml">
<classes>
<class name="test.cli.github2974.TwoGroupsTest"/>
</classes>
</test>
</suite>
12 changes: 12 additions & 0 deletions testng-core/src/test/resources/2974/parent_exclude.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="parent suite with default group" parallel="methods" thread-count="1">

<groups>
<run>
<exclude name="default_group"/>
</run>
</groups>
<suite-files>
<suite-file path="child.xml"/>
</suite-files>
</suite>
12 changes: 12 additions & 0 deletions testng-core/src/test/resources/2974/parent_include.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="parent suite with default group" parallel="methods" thread-count="1">

<groups>
<run>
<include name="default_group"/>
</run>
</groups>
<suite-files>
<suite-file path="child.xml"/>
</suite-files>
</suite>
1 change: 1 addition & 0 deletions testng-core/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<class name="test.github765.ExcludeSyntheticMethodsFromTemplateCallsTest"/>
<class name="test.github1405.TestExclusionOfMainMethod"/>
<class name="test.cli.CliTest"/>
<class name="test.cli.github2974.OverrideGroupsCliTest"/>
<class name="test.thread.ParallelSuiteTest"/>
<class name="test.simple.IncludedExcludedTest" />
<class name="test.reports.ReportTest" />
Expand Down
Loading