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

Filter-name should work on example names #1349

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,6 @@ open class SpecmaticJUnitSupport {
suggestionsData,
testConfig,
specificationPath = it,
filterName = filterName,
filterNotName = filterNotName,
specmaticConfig = specmaticConfig,
overlayContent = overlayContent
)
Expand Down Expand Up @@ -282,8 +280,6 @@ open class SpecmaticJUnitSupport {
it.branch,
it.specificationPath,
specmaticConfig?.security,
filterName,
filterNotName,
specmaticConfig = specmaticConfig,
overlayContent = overlayContent
)
Expand Down Expand Up @@ -454,8 +450,6 @@ open class SpecmaticJUnitSupport {
sourceRepositoryBranch: String? = null,
specificationPath: String? = null,
securityConfiguration: SecurityConfiguration? = null,
filterName: String?,
filterNotName: String?,
specmaticConfig: SpecmaticConfig? = null,
overlayContent: String = ""
): Pair<Sequence<ContractTest>, List<Endpoint>> {
Expand Down Expand Up @@ -497,26 +491,14 @@ open class SpecmaticJUnitSupport {
)
}

val filteredScenariosBasedOnName = selectTestsToRun(
feature.scenarios.asSequence(),
filterName,
filterNotName
) { it.testDescription() }
val filteredScenarios = filterUsing(
filteredScenariosBasedOnName,
feature.scenarios.asSequence(),
scenarioMetadataFilter,
scenarioMetadataExclusionFilter
) { it.toScenarioMetadata() }

val tests: Sequence<ContractTest> = feature
.copy(scenarios = filteredScenarios.toList())
.also {
if (it.scenarios.isEmpty())
logger.log("All scenarios were filtered out.")
else if (it.scenarios.size < feature.scenarios.size) {
logger.debug("Selected scenarios:")
it.scenarios.forEach { scenario -> logger.debug(scenario.testDescription().prependIndent(" ")) }
}
}
.generateContractTests(suggestions)

return Pair(tests, allEndpoints)
Expand Down Expand Up @@ -615,7 +597,7 @@ fun <T> selectTestsToRun(
} else
testScenarios

val filteredByNotName: Sequence<T> = if(!filterNotName.isNullOrBlank()) {
val filteredScenarios: Sequence<T> = if(!filterNotName.isNullOrBlank()) {
val filterNotNames = filterNotName.split(",").map { it.trim() }

filteredByName.filterNot { test ->
Expand All @@ -624,6 +606,18 @@ fun <T> selectTestsToRun(
} else
filteredByName

return filteredByNotName
val filteredScenariosCount = filteredScenarios.count()

when {
filteredScenariosCount == 0 -> logger.log("All scenarios were filtered out.")
filteredScenariosCount < testScenarios.count() -> {
logger.debug("Selected scenarios:")
filteredScenarios.forEach { scenario ->
logger.debug(getTestDescription(scenario).prependIndent(" "))
}
}
}

return filteredScenarios
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.specmatic.test

import io.specmatic.core.TestConfig
import io.specmatic.test.SpecmaticJUnitSupport.Companion.CONTRACT_PATHS
import io.specmatic.test.SpecmaticJUnitSupport.Companion.FILTER_NAME_PROPERTY
import io.specmatic.test.SpecmaticJUnitSupport.Companion.FILTER_NOT_NAME_PROPERTY
import io.specmatic.test.SpecmaticJUnitSupport.Companion.HOST
import io.specmatic.test.SpecmaticJUnitSupport.Companion.PORT
import io.specmatic.test.SpecmaticJUnitSupport.Companion.PROTOCOL
Expand Down Expand Up @@ -29,9 +32,7 @@ class SpecmaticJunitSupportTest {
"./src/test/resources/spec_with_parameterized_paths.yaml",
"",
"",
TestConfig(emptyMap(), emptyMap()),
filterName = null,
filterNotName = null
TestConfig(emptyMap(), emptyMap())
)
val specEndpoints = result.second
assertThat(specEndpoints.count()).isEqualTo(2)
Expand Down Expand Up @@ -151,6 +152,26 @@ class SpecmaticJunitSupportTest {
assertThat(registeredListeners).contains(ContractExecutionListener::class.java.name)
}

@Test
fun`should return the two tests other than the test with example name given in filterNotName` () {
System.setProperty(CONTRACT_PATHS, "./src/test/resources/spec_with_parameterized_paths.yaml")
System.setProperty(TEST_BASE_URL, "https://test.com")
System.setProperty(FILTER_NOT_NAME_PROPERTY, "BLACKLISTED")
val filteredTests = SpecmaticJUnitSupport().contractTest().map { it }.toList()
assertThat(filteredTests).hasSize(2)
assertThat(filteredTests.map { it.displayName }.toString()).doesNotContain("BLACKLISTED")
}

@Test
fun`should return only the test with example name given in filterName` () {
System.setProperty(CONTRACT_PATHS, "./src/test/resources/spec_with_parameterized_paths.yaml")
System.setProperty(TEST_BASE_URL, "https://test.com")
System.setProperty(FILTER_NAME_PROPERTY, "BLACKLISTED")
val filteredTests = SpecmaticJUnitSupport().contractTest().map { it }.toList()
assertThat(filteredTests).hasSize(1)
assertThat(filteredTests.map { it.displayName.toString() }[0]).contains("BLACKLISTED")
}

@AfterEach
fun tearDown() {
System.getProperties().keys.minus(initialPropertyKeys).forEach { println("Clearing $it"); System.clearProperty(it.toString()) }
Expand Down