Access ScalaTest test name from inside test?

后端 未结 3 1207
伪装坚强ぢ
伪装坚强ぢ 2021-02-13 23:30

Is it possible to access the name of the currently executing test, from within a ScalaTest test? (And how would I do it?)

Background:

I\'m testi

3条回答
  •  失恋的感觉
    2021-02-14 00:05

    Create your own trait, let say RichFreeSpec.

    trait RichFreeSpec extends Free {
      protected final class RichFreeSpecStringWrapper(name: scala.Predef.String) {
        def in(f: String => scala.Unit) {
          def f2 = f(name)
          new WordSpecStringWrapper(string).in(f2)
        }
      }  
    
      protected implicit def convertToRichFreeSpecStringWrapper(n: scala.Predef.String): = {
        new RichFreeSpecStringWrapper(n)
      }
    }
    

    Than just use:

    "sth" in { testName => 
       ...
     }
    

    Of course, you can go further and implement the full name hierarchy.

提交回复
热议问题