下面列出了org.junit.runner.Description#addChild() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testCounterIgnoresDescriptionsWithMethodNames() throws Exception
{
Description root = Description.createSuiteDescription(ROOT);
Description beforeStories = Description.createTestDescription(Object.class, BEFORE);
Description story = Description.createSuiteDescription(STORY);
Description beforeStory = Description.createTestDescription(Object.class, BEFORE);
Description scenario = Description.createSuiteDescription(SCENARIO);
root.addChild(beforeStories);
root.addChild(story);
story.addChild(beforeStory);
story.addChild(scenario);
Properties properties = mockPropertiesBeanInstantiation();
mockJUnitReportingRunnerInstantiation(root);
BddScenariosCounter.main(new String[0]);
verify(properties).put(RESOURCE_LOCATION, DEFAULT_STORY_LOCATION);
assertThat(getOutStreamContent(), containsString(1 + SEPARATOR + STORIES));
assertThat(getOutStreamContent(), containsString(1 + SEPARATOR + SCENARIOS));
assertThat(getOutStreamContent(), containsString(1 + SEPARATOR + SCENARIOS_WITH_EXAMPLES));
}
private void map(Description source, Description parent) {
for (Description child : source.getChildren()) {
Description mappedChild;
if (child.getMethodName() != null) {
mappedChild = Description.createSuiteDescription(String.format("%s [%s](%s)", child.getMethodName(), getDisplayName(), child.getClassName()));
parent.addChild(mappedChild);
if (!isTestEnabled(new TestDescriptionBackedTestDetails(source, child))) {
disabledTests.add(child);
} else {
enabledTests.add(child);
}
} else {
mappedChild = Description.createSuiteDescription(child.getClassName());
}
descriptionTranslations.put(child, mappedChild);
map(child, parent);
}
}
@Override
public Description getDescription()
{
final Description suite = Description.createSuiteDescription( this.testClass );
for ( final ScriptTestInstance file : this.testFiles )
{
final Description desc = Description.createTestDescription( file.getName(), file.getName() );
suite.addChild( desc );
for ( final ScriptTestMethod method : file.getTestMethods() )
{
final Description methodDesc = Description.createTestDescription( file.getName(), method.getName() );
desc.addChild( methodDesc );
}
}
return suite;
}
private void map(Description source, Description parent) {
for (Description child : source.getChildren()) {
Description mappedChild;
if (child.getMethodName() != null) {
mappedChild = Description.createSuiteDescription(String.format("%s [%s](%s)", child.getMethodName(), getDisplayName(), child.getClassName()));
parent.addChild(mappedChild);
if (!isTestEnabled(new TestDescriptionBackedTestDetails(source, child))) {
disabledTests.add(child);
} else {
enabledTests.add(child);
}
} else {
mappedChild = Description.createSuiteDescription(child.getClassName());
}
descriptionTranslations.put(child, mappedChild);
map(child, parent);
}
}
private void map(Description source, Description parent) {
for (Description child : source.getChildren()) {
Description mappedChild;
if (child.getMethodName() != null) {
mappedChild = Description.createSuiteDescription(String.format("%s [%s](%s)", child.getMethodName(), getDisplayName(), child.getClassName()));
parent.addChild(mappedChild);
if (!isTestEnabled(new TestDescriptionBackedTestDetails(source, child))) {
disabledTests.add(child);
} else {
enabledTests.add(child);
}
} else {
mappedChild = Description.createSuiteDescription(child.getClassName());
}
descriptionTranslations.put(child, mappedChild);
map(child, parent);
}
}
static Description makeDescription(Test test) {
if (test instanceof TestCase) {
TestCase tc = (TestCase) test;
return Description.createTestDescription(tc.getClass(), tc.getName(), getAnnotations(tc));
} else if (test instanceof TestSuite) {
TestSuite ts = (TestSuite) test;
String name = ts.getName() == null ? createSuiteDescription(ts) : ts.getName();
Description description = Description.createSuiteDescription(name);
int n = ts.testCount();
for (int i = 0; i < n; i++) {
Description made = makeDescription(ts.testAt(i));
description.addChild(made);
}
return description;
} else if (test instanceof Describable) {
Describable adapter = (Describable) test;
return adapter.getDescription();
} else if (test instanceof TestDecorator) {
TestDecorator decorator = (TestDecorator) test;
return makeDescription(decorator.getTest());
} else {
// This is the best we can do in this case
return Description.createSuiteDescription(test.getClass());
}
}
@Test
public void testFailingTest() throws Exception {
config = createConfig();
mockRunListener = mock(RunListener.class);
JUnit4Runner runner = createRunner(SampleFailingTest.class);
Description testDescription = Description.createTestDescription(SampleFailingTest.class,
"testThatAlwaysFails");
Description suiteDescription = Description.createSuiteDescription(SampleFailingTest.class);
suiteDescription.addChild(testDescription);
Result result = runner.run();
assertThat(result.getRunCount()).isEqualTo(1);
assertThat(result.getFailureCount()).isEqualTo(1);
assertThat(result.getIgnoreCount()).isEqualTo(0);
assertThat(extractOutput(stdoutByteStream))
.contains(
"1) testThatAlwaysFails("
+ SampleFailingTest.class.getName()
+ ")\n"
+ "java.lang.AssertionError: expected");
InOrder inOrder = inOrder(mockRunListener);
inOrder.verify(mockRunListener).testRunStarted(any(Description.class));
inOrder.verify(mockRunListener).testStarted(any(Description.class));
inOrder.verify(mockRunListener).testFailure(any(Failure.class));
inOrder.verify(mockRunListener).testFinished(any(Description.class));
inOrder.verify(mockRunListener).testRunFinished(any(Result.class));
}
private Description describeRepeatTest(FrameworkMethod method) {
int times = method.getAnnotation(Repeat.class).value();
Description description = Description.createSuiteDescription(
testName(method) + " [" + times + " times]",
method.getAnnotations());
for (int i = 1; i <= times; i++) {
description.addChild(Description.createTestDescription(
getTestClass().getJavaClass(),
testName(method) + "-" + i));
}
return description;
}
@Override
public Description getDescription() {
if (properties.getProperty(CLOUD_PROVIDER, CLOUDPROVIDER_LOCAL).equals(CLOUDPROVIDER_AWS)) {
Description suiteDescription =
Description.createSuiteDescription(AWS_JUNIT_SUITE_DESCRIPTION);
suiteDescription.addChild(
Description.createTestDescription("groupName", AWS_JUNIT_GROUP_DESCRIPTION));
return suiteDescription;
} else {
return super.getDescription();
}
}
@Override
public Description getDescription() {
Description description = Description.createSuiteDescription(getName(), getRunnerAnnotations());
for (Runner child : getFilteredChildren()) {
description.addChild(describeChild(child));
}
return description;
}
private Description createFlatTestDescription(List<FrameworkMethod> methods)
{
Description flatDescription = Description.createSuiteDescription(getTestClass().getJavaClass());
for (FrameworkMethod method : methods)
{
flatDescription.addChild(describeChild(method));
}
return flatDescription;
}
public Description getDescription() {
Description description = Description.createSuiteDescription(testClass.getName(), testClass.getAnnotations());
for (JmesPathComplianceTest.ComplianceTest<T> complianceTest : getAllTests()) {
description.addChild(createDescription(complianceTest));
}
return description;
}
@Override
public Description getDescription() {
Description description = Description.createSuiteDescription(testClass);
for (Entry<String, List<String>> entry : testCases.entrySet()) {
for (String test : entry.getValue()) {
String testCase = entry.getKey();
Description childDescription = createDescription(testCase, test);
description.addChild(childDescription);
}
}
return description;
}
@Override
public Description getDescription() {
Description description = Description.createSuiteDescription("Automated Tests");
for (Runner child : getChildren()) {
description.addChild(describeChild(child));
}
return description;
}
@Test
public void testFailingInternationalCharsTest() throws Exception {
config = createConfig();
mockRunListener = mock(RunListener.class);
JUnit4Runner runner = createRunner(SampleInternationalFailingTest.class);
Description testDescription = Description.createTestDescription(
SampleInternationalFailingTest.class, "testFailingInternationalCharsTest");
Description suiteDescription = Description.createSuiteDescription(
SampleInternationalFailingTest.class);
suiteDescription.addChild(testDescription);
Result result = runner.run();
assertThat(result.getRunCount()).isEqualTo(1);
assertThat(result.getFailureCount()).isEqualTo(1);
assertThat(result.getIgnoreCount()).isEqualTo(0);
String output = new String(stdoutByteStream.toByteArray(), StandardCharsets.UTF_8);
// Intentionally swapped "Test 日\u672C." / "Test \u65E5本." to make sure that the "raw"
// character does not get corrupted (would become ? in both cases and we would not notice).
assertThat(output).contains("expected:<Test [Japan].> but was:<Test [日\u672C].>");
InOrder inOrder = inOrder(mockRunListener);
inOrder.verify(mockRunListener).testRunStarted(any(Description.class));
inOrder.verify(mockRunListener).testStarted(any(Description.class));
inOrder.verify(mockRunListener).testFailure(any(Failure.class));
inOrder.verify(mockRunListener).testFinished(any(Description.class));
inOrder.verify(mockRunListener).testRunFinished(any(Result.class));
}
/**
* 実行されるテストのdescriptionを取得します。
*
* @return description
*/
public Description getTestDescription() {
Description description = getDescription();
Description result = Description.createTestDescription(getTestClass().getJavaClass(), getName(),
getRunnerAnnotations());
for (Description d : description.getChildren()) {
result.addChild(d);
}
return result;
}
private static Description wrap(Description description) {
String name = description.getDisplayName();
String fixedName = deparametrizedName(name);
Description clonedDescription = Description.createSuiteDescription(fixedName, description.getAnnotations()
.toArray(new Annotation[0]));
for (Description child : description.getChildren()) {
clonedDescription.addChild(wrap(child));
}
return clonedDescription;
}
@Override
public Description getDescription() {
Description description = Description.createSuiteDescription(getName(), getRunnerAnnotations());
for (FrameworkMethod child : getFilteredChildren()) {
description.addChild(describeChild(child));
}
return description;
}
/**
* Creates a test suite description (a Description that returns true
* when {@link org.junit.runner.Description#isSuite()} is called.)
*/
protected static Description createTestSuiteDescription() {
Description testSuiteDescription = Description.createSuiteDescription("testSuite");
testSuiteDescription.addChild(Description.createSuiteDescription("testCase"));
return testSuiteDescription;
}
private Description createHierarchicalTestDescription(List<FrameworkMethod> methods)
{
Description hierarchicalDescription = Description.createSuiteDescription(getTestClass().getJavaClass());
for (FrameworkMethod method : methods)
{
Description currentLevel = hierarchicalDescription;
if (method instanceof EnhancedMethod)
{
EnhancedMethod enhancedMethod = (EnhancedMethod) method;
List<StatementBuilder> statementBuilder = enhancedMethod.getBuilder();
List<Object> builderData = enhancedMethod.getData();
for (int i = 0; i < statementBuilder.size(); i++)
{
StatementBuilder builder = statementBuilder.get(i);
String categoryName = builder.getCategoryName(builderData.get(i));
// check if hierarchical description has a child with that description
ArrayList<Description> currentLevelChildren = currentLevel.getChildren();
boolean found = false;
for (Description currentLevelChild : currentLevelChildren)
{
if (categoryName.equals(currentLevelChild.getDisplayName()))
{
found = true;
currentLevel = currentLevelChild;
break;
}
}
// create one if it's missing and set the new one as the current level, then dig deeper
if (!found)
{
Description newChild = Description.createSuiteDescription(categoryName);
currentLevel.addChild(newChild);
currentLevel = newChild;
}
}
// finally add the test method to lowest level
currentLevel.addChild(describeChild(method));
}
else
{
// it's just a default JUnit method, just add it as child
hierarchicalDescription.addChild(describeChild(method));
}
}
return hierarchicalDescription;
}