scalatestplus-play icon indicating copy to clipboard operation
scalatestplus-play copied to clipboard

[Question] OneBrowserPerSuite: How to prevent browser startup when skipping tests using this trait?

Open el-dom opened this issue 7 years ago • 2 comments

I'm in a scenario where unittests and fluentlenium integration tests are placed in the same module but need to be run seperately at different stages of deployment in different environments.

A Fluentlenium Spec might look like this

@IntegrationTest
class IntegrationSpec extends WordSpec with MustMatchers  with GuiceOneServerPerSuite with OneBrowserPerSuite {
 override def fakeApplication(): Application = FluentleniumSetup.app
 override def createWebDriver(): WebDriver = FluentleniumSetup.createWebDriver()
...
}

and a unit test like this

@UnitTest
class FooSpec extends WordSpec with MustMatchers with TypeCheckedTripleEquals {
...
}

To be able to seperate the test executions, this configuration in build.sbt was created:

lazy val ITest = config("i").extend(Test)
lazy val UTest = config("u").extend(Test)
testOptions in ITest += Tests.Argument(TestFrameworks.ScalaTest, "-l","foo.bar.UnitTest
testOptions in UTest += Tests.Argument(TestFrameworks.ScalaTest, "-l","foo.bar.IntegrationTest")

This way i can run sbt u:test and all suits annotated with @IntegrationTest will be skipped due to the exclude flag -l. But still, whenever test execution reaches a Test annotated with IntegrationSpec, a browser gets started and closes immediately without doing anything, because all tests in this Suite get skipped. Since there is no browser installed in the environment where u:test is meant to be executed, this attempt to start a browser breaks test execution. Which is unfortunate because it wouldn't be necessary to start a browser at all. Am i doing something wrong in my attempt to skip the Testsuits?

el-dom avatar Jun 30 '17 18:06 el-dom

So right now my last resort was to extend the OneBrowserPerSuite trait, override run, check for excludedTags and just return SucceededStatus if the suite should be skipped due to its annotations.

trait OneBrowserPerSuite extends org.scalatestplus.play.OneBrowserPerSuite { this: TestSuite with ServerProvider =>
  abstract override def run(testName: Option[String], args: Args): Status = {
    val excludedTags = this.getClass.getAnnotations.map(_.annotationType().getName).filter(args.filter.tagsToExclude.contains)
    if (excludedTags.nonEmpty) {
      new CompositeStatus(Set(SucceededStatus))
    } else {
      super.run(testName, args)
    }
  }
}

@marcospereira Would it make sense to do something similar for org.scalatestplus.play.OneBrowserPerSuite? I'm aware tests get skipped even without this hack, and most people wouldn't care about the browser startup, but i think it would make sense to prevent the startup, since it's unnecessary when the suite shall be skipped anyway.

el-dom avatar Jul 07 '17 10:07 el-dom

having poked around to trying to decouple from the async tests, I believe the issue is that the current hooks use:

abstract override def run(testName: Option[String], args: Args): Status

which gets executed even if a test is filtered out. A course of action may be to use BeforeAndAfterAll to start/stop (I'll be opening a PR for the non-browser suites with this), and override either:

  • testDataFor
  • runTest

to include the driver instance in the config map

kwinter avatar Apr 28 '20 17:04 kwinter