了解测试套件
Understanding test suites
我正在学习 scalatest,对套件有疑问。我想测试一个
class ByteSource(val src: Array[Byte])
从逻辑上讲,我将测试用例分为以下两个:
- 空字节源
- 非空字节源
问题是像这样将案例分成不同的套件是否正确:
class ByteSourceTest extends FunSpec with Matchers{
describe("empty byte source operations"){
//...
}
describe("non-empty byte source operations"){
//...
}
}
或者FunSpec
不太适合这种情况?
FunSpec
旨在提供最小结构,因此这里没有硬性规定。自以为是的结构的一个例子是 WordSpec
。我会提出的一个建议是通过使用外部 describe("A ByteSource")
:
来清楚地识别测试的主题
class ByteSourceTest extends FunSpec with Matchers {
describe("A ByteSource") {
describe("when empty") {
val byteSource = new ByteSource(new Array[Byte](0))
it("should have size 0") {
assert(byteSource.src.size == 0)
}
it("should produce NoSuchElementException when head is invoked") {
assertThrows[NoSuchElementException] {
byteSource.src.head
}
}
}
describe("when non-empty") {
val byteSource = new ByteSource(new Array[Byte](10))
it("should have size > 0") {
assert(byteSource.src.size > 0)
}
it("should not produce NoSuchElementException when head is invoked") {
noException shouldBe thrownBy {
byteSource.src.head
}
}
}
}
}
让测试对象的输出看起来像自然语言的规范:
A ByteSource
when empty
- should have size 0
- should produce NoSuchElementException when head is invoked
when non-empty
- should have size > 0
- should not produce NoSuchElementException when head is invoked
我正在学习 scalatest,对套件有疑问。我想测试一个
class ByteSource(val src: Array[Byte])
从逻辑上讲,我将测试用例分为以下两个:
- 空字节源
- 非空字节源
问题是像这样将案例分成不同的套件是否正确:
class ByteSourceTest extends FunSpec with Matchers{
describe("empty byte source operations"){
//...
}
describe("non-empty byte source operations"){
//...
}
}
或者FunSpec
不太适合这种情况?
FunSpec
旨在提供最小结构,因此这里没有硬性规定。自以为是的结构的一个例子是 WordSpec
。我会提出的一个建议是通过使用外部 describe("A ByteSource")
:
class ByteSourceTest extends FunSpec with Matchers {
describe("A ByteSource") {
describe("when empty") {
val byteSource = new ByteSource(new Array[Byte](0))
it("should have size 0") {
assert(byteSource.src.size == 0)
}
it("should produce NoSuchElementException when head is invoked") {
assertThrows[NoSuchElementException] {
byteSource.src.head
}
}
}
describe("when non-empty") {
val byteSource = new ByteSource(new Array[Byte](10))
it("should have size > 0") {
assert(byteSource.src.size > 0)
}
it("should not produce NoSuchElementException when head is invoked") {
noException shouldBe thrownBy {
byteSource.src.head
}
}
}
}
}
让测试对象的输出看起来像自然语言的规范:
A ByteSource
when empty
- should have size 0
- should produce NoSuchElementException when head is invoked
when non-empty
- should have size > 0
- should not produce NoSuchElementException when head is invoked