I'm trying to use the @BeforeSuite annotation in my tests to create some Data that will be used across all tests, therefore I only want to create it once and then be able to access it from all of my tests that run.
I'm seeing an issue where, SOMETIMES, my data gets created and is accessible within my tests, and other times the data is created but the data object is null
when the test is executing.
Why would it only work sometimes and not others?
Here are my slimmed down pieces of relevant code.
class DataSetup {
@Synchronized
fun getData(): DataProfile {
var user: User("Bill", "Smyth")
var address: Address("123 Fake St", "Australia")
return DataProfile(user, address)
}
fun printDataProfile(profile: DataProfile) {
println(
"DataProfile:: User: ${user.name}"
}
}
data class DataProfile(
var user: User,
var address: Address
)
I have another base class where I do the setup within a @BeforeSuite method.
open class TestBase {
lateinit var profile: DataProfile
@BeforeSuite(enabled = true, groups = ["smoke", "regression"])
fun create_data(testContext: ITestContext) {
profile = DataSetup().getData()
My test file then inherits from this base class.
class UserDetailsTest : TestBase() {
@Test(description = "Check User Details", groups = ["regression"])
fun givenCheckList_WhenRetrieved_ThenReturns200() {
println(profile) // THIS IS SOMETIMES NULL, WHY??
}
}
The gradle task I have defined in my build.gradle file.
tasks.register<Test>("regression") {
useTestNG() {
testLogging.showStandardStreams = true
includeGroups ("regression")
}
}
Is this the correct way to go about using shared data between tests? Or should I be using ITestContext in all of my test files?
This is all still very new to me so any help to understand is much appreciated!