Skip to content

Commit

Permalink
# 46 - Introduce Before|After All and Before|AfterEachTest logic
Browse files Browse the repository at this point in the history
* Refactored Before and After logic to BeforeSuite and AfterSuite.
  • Loading branch information
miroslavpojer committed Dec 5, 2023
1 parent 67736f0 commit bcf8b7a
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"$schema": "http://json-schema.org/draft-06/schema#",
"$ref": "#/definitions/suiteAfter",
"$ref": "#/definitions/afterSuite",

"definitions": {
"suiteAfter": {
"afterSuite": {
"type": "object",
"additionalProperties": true,
"properties": {
Expand All @@ -23,8 +23,8 @@
"name",
"methods"
],
"title": "SuiteAfter",
"description": "Defines a suite with its associated methods to be executed after the main tests."
"title": "AfterSuite",
"description": "Defines a set of methods to be executed after the connected Suite."
},
"Method": {
"type": "object",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"$schema": "http://json-schema.org/draft-06/schema#",
"$ref": "#/definitions/suiteBefore",
"$ref": "#/definitions/beforeSuite",

"definitions": {
"suiteBefore": {
"beforeSuite": {
"type": "object",
"additionalProperties": true,
"properties": {
Expand All @@ -23,8 +23,8 @@
"name",
"methods"
],
"title": "SuiteBefore",
"description": "Defines a suite with its associated methods."
"title": "BeforeSuite",
"description": "Defines a set of methods to be executed before the connected Suite."
},
"Method": {
"type": "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ case class ProjectLoadFailedException() extends Exception("Problems during proje
case class SuiteLoadFailedException(detail: String)
extends Exception(s"Problems during project loading. Details: $detail")

case class SuiteBeforeFailedException(detail: String)
case class BeforeSuiteFailedException(detail: String)
extends Exception(s"Problems during running before suite logic. Details: $detail")

case class UndefinedHeaderTypeException(undefinedType: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,28 +137,28 @@ object SuiteFactory {
// TODO - code proposal - will be solved in #4
// val functions: Map[String, String] = loadJsonSuiteFunctions(suiteFilePath, environmentMap)

val beforeActions: Option[BeforeTestSet] = loadJsonSuite[BeforeTestSet](
val beforeSuiteActions: Option[BeforeTestSet] = loadJsonSuite[BeforeTestSet](
suiteFilePath,
suiteName,
environmentMap ++ suiteConstants.constants,
ScAPIJsonSchema.SUITE_BEFORE,
"before",
parseToSuiteBefore
ScAPIJsonSchema.BEFORE_SUITE,
"beforeSuite",
parseToBeforeSuite
)
val afterActions: Option[AfterTestSet] = loadJsonSuite[AfterTestSet](
val afterSuiteActions: Option[AfterTestSet] = loadJsonSuite[AfterTestSet](
suiteFilePath,
suiteName,
environmentMap ++ suiteConstants.constants,
ScAPIJsonSchema.SUITE_AFTER,
"after",
parseToSuiteAfter
ScAPIJsonSchema.AFTER_SUITE,
"afterSuite",
parseToAfterSuite
)

JsonSchemaValidator.validate(suitePath, ScAPIJsonSchema.SUITE)
val jsonString: String = JsonUtils.stringFromPath(suitePath)
val notResolvedSuite: TestSet = parseToSuite(jsonString)
val resolvedSuite: TestSet = notResolvedSuite.resolveReferences(environmentMap ++ suiteConstants.constants)
Suite(resolvedSuite, beforeActions, afterActions)
Suite(resolvedSuite, beforeSuiteActions, afterSuiteActions)
}

/**
Expand Down Expand Up @@ -221,24 +221,24 @@ object SuiteFactory {
}

/**
* Method to parse a SuiteBefore instance from the given JSON string.
* Method to parse a BeforeSuite instance from the given JSON string.
*
* @param jsonString The JSON string to be parsed.
* @return A SuiteBefore instance.
* @return A BeforeSuite instance.
*/
private def parseToSuiteBefore(jsonString: String): BeforeTestSet = {
import SuiteBeforeJsonProtocol.suiteBeforeFormat
private def parseToBeforeSuite(jsonString: String): BeforeTestSet = {
import BeforeSuiteJsonProtocol.beforeSuiteFormat
jsonString.parseJson.convertTo[BeforeTestSet]
}

/**
* Method to parse a SuiteAfter instance from the given JSON string.
* Method to parse a AfterSuite instance from the given JSON string.
*
* @param jsonString The JSON string to be parsed.
* @return A SuiteAfter instance.
* @return A AfterSuite instance.
*/
private def parseToSuiteAfter(jsonString: String): AfterTestSet = {
import SuiteAfterJsonProtocol.suiteAfterFormat
private def parseToAfterSuite(jsonString: String): AfterTestSet = {
import AfterSuiteJsonProtocol.afterSuiteFormat
jsonString.parseJson.convertTo[AfterTestSet]
}

Expand Down Expand Up @@ -299,27 +299,27 @@ object SuiteConstantJsonProtocol extends DefaultJsonProtocol {
}

/**
* Object that provides implicit JSON format for SuiteBefore class.
* Object that provides implicit JSON format for BeforeSuite class.
*/
object SuiteBeforeJsonProtocol extends DefaultJsonProtocol {
object BeforeSuiteJsonProtocol extends DefaultJsonProtocol {
implicit val headerFormat: RootJsonFormat[Header] = jsonFormat2(Header)
implicit val paramFormat: RootJsonFormat[Param] = jsonFormat2(Param)
implicit val testActionFormat: RootJsonFormat[Action] = jsonFormat4(Action)
implicit val responseActionFormat: RootJsonFormat[ResponseAction] = ResponseActionJsonProtocol.ResponseActionJsonFormat
implicit val methodFormat: RootJsonFormat[Method] = jsonFormat4(Method)
implicit val suiteBeforeFormat: RootJsonFormat[BeforeTestSet] = jsonFormat2(BeforeTestSet)
implicit val beforeSuiteFormat: RootJsonFormat[BeforeTestSet] = jsonFormat2(BeforeTestSet)
}

/**
* Object that provides implicit JSON format for SuiteAfter class.
* Object that provides implicit JSON format for AfterSuite class.
*/
object SuiteAfterJsonProtocol extends DefaultJsonProtocol {
object AfterSuiteJsonProtocol extends DefaultJsonProtocol {
implicit val headerFormat: RootJsonFormat[Header] = jsonFormat2(Header)
implicit val paramFormat: RootJsonFormat[Param] = jsonFormat2(Param)
implicit val testActionFormat: RootJsonFormat[Action] = jsonFormat4(Action)
implicit val responseActionFormat: RootJsonFormat[ResponseAction] = ResponseActionJsonProtocol.ResponseActionJsonFormat
implicit val methodFormat: RootJsonFormat[Method] = jsonFormat4(Method)
implicit val suiteAfterFormat: RootJsonFormat[AfterTestSet] = jsonFormat2(AfterTestSet)
implicit val afterSuiteFormat: RootJsonFormat[AfterTestSet] = jsonFormat2(AfterTestSet)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object ScAPIJsonSchema {
val ENVIRONMENT: URL = getClass.getResource("/schema/env.schema.json")
val SUITE: URL = getClass.getResource("/schema/suite.schema.json")
val SUITE_CONSTANTS: URL = getClass.getResource("/schema/constants.schema.json")
val SUITE_BEFORE: URL = getClass.getResource("/schema/before.schema.json")
val SUITE_AFTER: URL = getClass.getResource("/schema/after.schema.json")
val BEFORE_SUITE: URL = getClass.getResource("/schema/beforeSuite.schema.json")
val AFTER_SUITE: URL = getClass.getResource("/schema/afterSuite.schema.json")
}

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package africa.absa.testing.scapi.model.suite
*
* @constructor Create a new suite bundle with a suite and optional "before" and "after" actions.
* @param suite The core suite of tests to be run.
* @param suiteBefore An optional SuiteBefore object, representing any setup actions to be run before the suite.
* @param suiteAfter An optional SuiteAfter object, representing any teardown actions to be run after the suite.
* @param beforeSuite An optional BeforeSuite object, representing any setup actions to be run before the suite.
* @param afterSuite An optional AfterSuite object, representing any teardown actions to be run after the suite.
*/
case class Suite(suite: TestSet, suiteBefore: Option[BeforeTestSet] = None, suiteAfter: Option[AfterTestSet] = None)
case class Suite(suite: TestSet, beforeSuite: Option[BeforeTestSet] = None, afterSuite: Option[AfterTestSet] = None)
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class SuitePreAndPostProcessing(name: String, methods: Set[Method]) {
* Method to resolve references within the before methods instance.
*
* @param references A map containing the references to be resolved.
* @return A new SuiteBefore instance where all references are resolved.
* @return A new BeforeSuite instance where all references are resolved.
*/
def resolveReferences(references: Map[String, String]): SuitePreAndPostProcessing
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package africa.absa.testing.scapi.model.suite.types
object SuiteResultType extends Enumeration {
type SuiteResultType = Value

val BeforeTestSet: SuiteResultType.Value = Value("before-test-set")
val TestSet: SuiteResultType.Value = Value("test-set")
val AfterTestSet: SuiteResultType.Value = Value("after-test-set")
val BeforeSuiteResult: SuiteResultType.Value = Value("before-suite-result")
val TestResult: SuiteResultType.Value = Value("test-result")
val AfterSuiteResult: SuiteResultType.Value = Value("after-suite-result")
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ object StdOutReporter {

printHeader("Simple Text Report")

val successCount = testResults.count(r => r.isSuccess && r.resultType == SuiteResultType.TestSet)
val failureCount = testResults.count(r => !r.isSuccess && r.resultType == SuiteResultType.TestSet)
val successCount = testResults.count(r => r.isSuccess && r.resultType == SuiteResultType.TestResult)
val failureCount = testResults.count(r => !r.isSuccess && r.resultType == SuiteResultType.TestResult)

addToReport(s"Number of tests run: ${successCount + failureCount}")
addToReport(s"Number of successful tests: $successCount")
addToReport(s"Number of failed tests: $failureCount")

if (testResults.nonEmpty) {
val suiteSummary = testResults
.filter(_.resultType == SuiteResultType.TestSet)
.filter(_.resultType == SuiteResultType.TestResult)
.groupBy(_.suiteName).map {
case (suiteName, results) =>
(suiteName, results.size, results.count(_.isSuccess))
Expand All @@ -87,7 +87,7 @@ object StdOutReporter {
printTableRowSplitter()
addToReport(s"| %-${maxSuiteLength}s | %-${maxTestLength}s | %-13s | %-7s | %-${maxTestCategoriesLength}s | ".format("Suite Name", "Test Name", "Duration (ms)", "Status", "Categories"))
printTableRowSplitter()
val resultsList = testResults.filter(_.resultType == SuiteResultType.TestSet)
val resultsList = testResults.filter(_.resultType == SuiteResultType.TestResult)
resultsList.zipWithIndex.foreach { case (result, index) =>
val duration = result.duration.map(_.toString).getOrElse("NA")
addToReport(s"| %-${maxSuiteLength}s | %-${maxTestLength}s | %13s | %-7s | %-${maxTestCategoriesLength}s | ".format(
Expand All @@ -106,9 +106,9 @@ object StdOutReporter {
testResults.filter(!_.isSuccess).foreach { result =>
addToReport(s"Suite: ${result.suiteName}")
result.resultType match {
case SuiteResultType.BeforeTestSet => addToReport(s"Before: ${result.name}")
case SuiteResultType.TestSet => addToReport(s"Test: ${result.name}")
case SuiteResultType.AfterTestSet => addToReport(s"After: ${result.name}")
case SuiteResultType.BeforeSuiteResult => addToReport(s"BeforeSuite: ${result.name}")
case SuiteResultType.TestResult => addToReport(s"Test: ${result.name}")
case SuiteResultType.AfterSuiteResult => addToReport(s"AfterSuite: ${result.name}")
}
addToReport(s"Error: ${result.errorMsg.getOrElse("No details available")}")
addToReport(s"Duration: ${result.duration.getOrElse("NA")} ms")
Expand Down
Loading

0 comments on commit bcf8b7a

Please sign in to comment.