groovy 如何重用测试的某些部分?

unftdfkk  于 2024-01-06  发布在  其他
关注(0)|答案(4)|浏览(205)

我需要实现以下逻辑:
1.做点什么
1.检查逻辑
1.做点别的
1.相似校验逻辑
我使用when/then块进行简单的测试。但我真的不知道如何实现更复杂的一个(如上所述)+我想尽可能多地重用代码。但与块它变得更复杂的实现

nle07wnf

nle07wnf1#

我在Spock中采用了几种方法来重用代码。

特性级别在setup:block中创建一个闭包,你可以将它当作一个方法,只对这个特性可用。

  1. def "test"() {
  2. setup:
  3. def containsCat = {String it -> it.contains('cat')}
  4. expect:
  5. !containsCat('I love my dog')
  6. containsCat('I love my cat')
  7. }
  8. def "test that cannot reference containsCat(String)"() {
  9. // Test stuff
  10. }

字符串

Spec Class Level虽然可以使用@Shared闭包,但我更喜欢使用私有helper方法,除非helper逻辑只有一两行。

  1. class tester extends Specification {
  2. @Shared
  3. def containsDog = {String it -> it.contains('dog')}
  4. private containsCat(String inputString) {
  5. inputString.contains('cat')
  6. }
  7. def "test"(String myPet) {
  8. expect: containsCat(myPet)
  9. where: myPet = 'I love my cat'
  10. }
  11. def "test2"() {
  12. expect: containsDog(mySistersPet)
  13. where: mySistersPet = 'I love my dog'
  14. }

套餐级别

我有一组类,它们都可以从共享一个微型测试框架中受益。我的偏好是使用trait。它们可以包含除了特性测试本身之外的任何代码。如果trait将引用来自测试本身的数据,请确保创建一个抽象方法,以便确保trait引用数据。

  1. trait petTester {
  2. private containsDog(String inputString) {
  3. inputString.contains('dog')
  4. }
  5. private containsCat(String inputString) {
  6. inputString.contains('cat')
  7. }
  8. }
  9. class myPetTester extends Specification implements petTester {
  10. def "test"(String myPet) {
  11. expect: containsCat(myPet)
  12. where: myPet = 'I love my cat'
  13. }
  14. }
  15. class mySistersPetTester extends Specification implements petTester {
  16. def "test2"() {
  17. expect: containsDog(mySistersPet)
  18. where: mySistersPet = 'I love my dog'
  19. }
  20. }

展开查看全部
iyfjxgzm

iyfjxgzm2#

如果你想运行相同的测试两次,只是改变一些参数,你可以使用where

  1. def "foo"(Boolean barIsEnabled) {
  2. when:
  3. myService.testBar(barIsEnabled)
  4. then:
  5. myService.readBar() == "123456"
  6. where: "this code shoud work with bar enabled or disabled"
  7. barIsEnabled | ignored
  8. true | _
  9. false | _
  10. }

字符串
参考号:http://spockframework.github.io/spock/docs/1.0/data_driven_testing.html
如果你只是想重用then逻辑,创建一个私有方法,并在其中添加许多assert

  1. def "foo"() {
  2. when:
  3. def pc = shop.buyPc()
  4. then:
  5. matchesPreferredConfiguration(pc)
  6. }
  7. void matchesPreferredConfiguration(pc) {
  8. assert pc.vendor == "Sunny"
  9. assert pc.clockRate >= 2333
  10. assert pc.ram >= 4096
  11. assert pc.os == "Linux"
  12. }


参考:http://spockframework.github.io/spock/docs/1.0/spock_primer.html#_helper_methods

展开查看全部
41ik7eoe

41ik7eoe3#

你也可以使用interaction { doStuff() }
然而,如果你发现你的doStuff()很大,并且你的许多测试都使用相同的交互方法,那么可能是时候考虑将生产类中的一些代码移动到一个单独的类中,然后有一个期望,即你的测试类调用你的新类。

n1bvdmb6

n1bvdmb64#

Spock文档中的例子非常好,但奇怪的是对我来说不太有效。https://spockframework.org/spock/docs/1.0/spock_primer.html#_helper_methods
我需要将\添加到他们的示例中

  1. def matchesPreferredConfiguration(pc) {
  2. pc.vendor == "Sunny" \
  3. && pc.clockRate >= 2333 \
  4. && pc.ram >= 4096 \
  5. && pc.os == "Linux"
  6. }

字符串

相关问题