문제

나는 주로 Scala와 Java로 프로그램되어 Scala에서 Scalatest와 Junit을 단위 테스트를 위해 사용합니다. 동일한 인터페이스/특성의 여러 구현에 동일한 테스트를 적용하고 싶습니다. 아이디어는 인터페이스 계약이 시행되었는지 확인하고 Liskov 대체 원칙을 확인하는 것입니다.

예를 들어, 목록 구현을 테스트 할 때 테스트에는 다음이 포함될 수 있습니다.

  • 크기가 0 인 경우에만 인스턴스가 비어 있어야합니다.
  • Clear를 호출 한 후 크기는 0이어야합니다.
  • 목록 중간에 요소를 추가하면 RHS 요소의 색인이 증가합니다.
  • 등.

모범 사례는 무엇입니까?

도움이 되었습니까?

해결책

This sounds like it could be a job for shared tests. Shared tests are tests that are shared by different fixture objects. I.e., the same test code is run on different data. ScalaTest does have support for that. Search for "shared tests" in the documentation of your favorite style trait that represents tests as functions (Spec, WordSpec, FunSuite, FlatSpec, etc.). An example is the syntax for FlatSpec:

it should behave like emptyList

See Sharing Tests in the FlatSpec documentation

다른 팁

In Java/JUnit, I generally handle this by having an abstract testcase from which tests for the specific test class inherit all the tests and have a setup method instantiating the implementation. I can't watch the video abyx posted right now, but I suspect it's this general idea.

Another interesting possibility if you don't mind introducing yet another testing framework would be to use JDave Specification classes.

I haven't tried using either of these with Scalatest or with Scala traits and implementations, but it should be possible to do something similar.

Contract tests are easy to do with JUnit 4, here's a video by Ben Rady.

For Scala, strongly consider ScalaCheck. All of those contracts are expressible as one-line specifications in ScalaCheck. When run, ScalaCheck will generate a configurable number of sample inputs randomly, and check that all of the specifications hold. It's about the most semantically dense way possible to create unit tests.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top