【问题标题】:How to prioritize tests in scala?如何在scala中对测试进行优先级排序?
【发布时间】:2019-09-04 10:07:23
【问题描述】:

我正在使用 scalatest 的 FunSuite 运行我的测试。我想定义 2 个单独的测试类,它们有一些共同的测试,所以我创建了一个 BaseTest 类,我在 MainTest1MainTest2 中扩展了它,在其中我定义了额外的特定测试(参见下面的代码 sn-ps )。我希望MainTest1MainTest2 中的测试以特定顺序执行,例如测试 4、2、1 和 3。我该如何实现?

class BaseTest extends FunSuite with BeforeAndAfter{

    test("Check Content of file){

    //code to check content of a file
    }

    test("File is not empty"){

    //code to check file is not empty
    }

    test("Check a particular word in file"){

    //code to check particular word in file
    }

    }

class MainTest1 extends BaseTest{

        test("Check create file"){

        //code to check file creation
        }
    }

class MainTest2 extends BaseTest{

    test("Check download file"){

    //code to check file downloaded properly
    }
}

【问题讨论】:

  • 写一个测试方法,把test4, test2, test1, test3放在里面。
  • 那么如果test4失败,test2,test3..不会执行
  • 为什么需要以特定顺序执行测试?这似乎是一种反模式。另外,请提供一个最小的代码示例,以确保我们理解您的问题。
  • 我已经添加了代码sn-p,请检查
  • 您可以在BaseTest 中定义test1()test2() etc 方法,以test("blah") { ... } 作为它们的主体。然后,您可以从MainTest1MainTest2 依次调用这些方法。一旦问题不再暂停,我将提供完整的代码示例。

标签: scala scalatest


【解决方案1】:

您可以将测试定义为方法,并按照您认为合适的顺序从子类中调用它们。

trait BaseTest extends FunSuite {

    protected def test1(): Unit = test("This is test 1") { ... }

    protected def test2(): Unit = test("This is test 1") { ... }

}

class MainTest1 extends BaseTest {

    // Arrange these is any order you see fit
    test3()
    test2()
    test1()

    protected def test3(): Unit = test("This is a test specific to MainTest1") { ... }

}

class MainTest2 extends BaseTest {

    // Arrange these is any order you see fit
    test2()
    test3()
    test1()

    protected def test3(): Unit = test("This is a test specific to MainTest2") { ... }

}

【讨论】:

  • 这里,问题是如果test3()失败了,test2() & test1()就不会执行,如何解决呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
相关资源
最近更新 更多