标签: zio-test

有没有办法在布尔 ZIO 测试中添加描述性断言消息

我有几个Booleans我想测试,比如

assert(g8Exists, equalTo(true)) &&
assert(projectExists, equalTo(true)) &&
assert(testenvExists, equalTo(true)) ...
Run Code Online (Sandbox Code Playgroud)

如果一个失败,我得到的只是:

false did not satisfy equalTo(true)
Run Code Online (Sandbox Code Playgroud)

不知道哪条线失败了。有没有办法添加描述性的断言消息。例如:

assert(g8Exists, equalTo(true), "g8Exists")
Run Code Online (Sandbox Code Playgroud)

或首选:

assertTrue(g8Exists, "g8Exists")
Run Code Online (Sandbox Code Playgroud)

会导致

false did not satisfy equalTo(true) - g8Exists
Run Code Online (Sandbox Code Playgroud)

或者有没有更好的测试方法Booleans

scala zio zio-test

6
推荐指数
1
解决办法
170
查看次数

如何强制按顺序运行 ZIO 测试

我想按顺序运行两个集成测试。如何在ZIO Test 中实现这一点?

这是套房:

suite("Undeploy a Package")(
    testM("There is a Package") {
      PackageDeployer.deploy(pckg) *> // first deploy
        assertM(PackageUndeployer.undeploy(pckg), equalTo(StatusCode.NoContent))
    },
    testM(s"There is no Package") {
        assertM(PackageUndeployer.undeploy(pckg), equalTo(StatusCode.NotFound))
    })
Run Code Online (Sandbox Code Playgroud)

ZIO Test并行运行这两个测试。有没有办法强制它们按顺序运行?

integration-testing scala zio zio-test

5
推荐指数
1
解决办法
415
查看次数

如何使用 zio-test 测试异常情况

我有以下功能,我想测试:

def people(id: Int): RIO[R, People]
Run Code Online (Sandbox Code Playgroud)

如果有一个,这个函数会返回 People id,resp。如果不是,则失败,例如:

IO.fail(ServiceException(s"No People with id $id"))
Run Code Online (Sandbox Code Playgroud)

快乐的案例有效,例如:

suite("Get a Person for an ID") (
    testM("get Luke Skywalker") {
      for {
        peopleRef <- Ref.make(Vector(People()))
        luke <- Swapi.>.people(1).provide(Test(peopleRef))
      } yield assert(luke, equalTo(People()))
    },
Run Code Online (Sandbox Code Playgroud)

但是我如何测试失败案例呢?我尝试了不同的东西,主要是类型不匹配。这是一个尝试:

    testM("get not existing People") {
      (for {
        peopleRef <- Ref.make(Vector(People()))
        failure = Swapi.>.people(2).provide(Test(peopleRef))
      } yield assertM(failure, fail(Cause.die(ServiceException(s"No People with id 2")))
    }
  )
Run Code Online (Sandbox Code Playgroud)

scala zio zio-test

4
推荐指数
6
解决办法
1664
查看次数

如何断言选项包含 zio-test 中的某些内容?

我试过

assert(anOption)(contains("x"))
Run Code Online (Sandbox Code Playgroud)

但这仅适用于 Iterables,例如 List 或 Seq。

scala zio zio-test

4
推荐指数
1
解决办法
404
查看次数

How to ignore a Suite or a Test in ZIO Test?

I could not find anything on how to ignore a Suite or a Test with ZIO Test.

Whether in an example nor in the documentation (https://zio.dev/docs/usecases/usecases_testing)

There is an ignored in the test package object:

  /**
   * Creates an ignored test result.
   */
  final val ignored: ZTest[Any, Nothing, Nothing] =
    ZIO.succeed(TestSuccess.Ignored)
Run Code Online (Sandbox Code Playgroud)

But how can it be used in your code?

I tried different things, but with no success.

scala zio zio-test

3
推荐指数
1
解决办法
393
查看次数

如何断言错误类型中的单个字段?

假设我有这样的代码:

final case class CustomException(errorCode: Int, id: UUID) extends Throwable

val logic: ZIO[Any, Throwable, Unit] = ???
Run Code Online (Sandbox Code Playgroud)

我想使用 ZIO Test 来检查特定的错误情况

val checkForTimeout = testM("Logic should time out") {
  for {
    result <- logic.flip
  } yield assert(result, isSubtype[CustomException](???))
}
Run Code Online (Sandbox Code Playgroud)

我想做的是检查该errorCode字段的特定值。但 ZIO Test 中现有的组合器似乎只允许我检查完整的对象。我只想_.errorCode在忽略时检查_.id,这意味着equalTo对于这个用例来说不是一个足够好的组合器。

我该如何解决这个问题?

scala zio zio-test

2
推荐指数
1
解决办法
518
查看次数

使用 ZIO2 和 zio-test 断言效果失败

我是 Scala / ZIO 2 世界的初学者,我正在尝试为一个简单的服务编写一些测试。

所以我有这个方法:

def validate(id: String): ZIO[Any, Throwable, Unit] = {
  if (id == "invalid-id") {
    ZIO.fail("Invalid id")
  }
}
Run Code Online (Sandbox Code Playgroud)

我尝试了几件事,但主要是尝试使用isFailurefails断言:

 suite("My suite")(
    test("When id is valid") { // This passes
      for {
        result <- validate("valid-id")
      } yield assertTrue(result == ())
    },
    test("when id is not valid") { 
      for {
        result <- validate("invalid-id")
      } yield assertTrue(isFailure(result)) // This doesn't even compile
    }
  )
Run Code Online (Sandbox Code Playgroud)

如何测试效果失败的情况?

我在用:

Scala: "3.2.1"
zio: "2.0.4"
zio-test: "2.0.5"
Run Code Online (Sandbox Code Playgroud)

scala zio zio-test

1
推荐指数
1
解决办法
535
查看次数

标签 统计

scala ×7

zio ×7

zio-test ×7

integration-testing ×1