|
| 1 | +test_that("otel instrumentation works", { |
| 2 | + skip_if_not_installed("otelsdk") |
| 3 | + |
| 4 | + record <- with_otel_record({ |
| 5 | + test_that("testing is traced", { |
| 6 | + expect_equal(1, 1) |
| 7 | + expect_error(stop("expected error")) |
| 8 | + }) |
| 9 | + test_that("all expectations are recorded", { |
| 10 | + expect_equal(1, 1) |
| 11 | + expect_true(TRUE) |
| 12 | + expect_length(1:3, 3) |
| 13 | + expect_warning(warning("expected warning")) |
| 14 | + expect_error(stop("expected error")) |
| 15 | + }) |
| 16 | + }) |
| 17 | + |
| 18 | + traces <- record$traces |
| 19 | + expect_length(traces, 2L) |
| 20 | + span <- traces[[1L]] |
| 21 | + expect_equal( |
| 22 | + span$name, |
| 23 | + "test that otel instrumentation works / testing is traced" |
| 24 | + ) |
| 25 | + expect_equal(span$instrumentation_scope$name, "org.r-lib.testthat") |
| 26 | + span <- traces[[2L]] |
| 27 | + expect_equal(span$attributes[["test.status"]], "pass") |
| 28 | + expect_equal(span$attributes[["test.expectations.total"]], 5) |
| 29 | + expect_equal(span$attributes[["test.expectations.passed"]], 5) |
| 30 | + expect_equal(span$attributes[["test.expectations.failed"]], 0) |
| 31 | + expect_equal(span$attributes[["test.expectations.error"]], 0) |
| 32 | + expect_equal(span$attributes[["test.expectations.skipped"]], 0) |
| 33 | + expect_equal(span$attributes[["test.expectations.warning"]], 0) |
| 34 | + expect_equal(span$status, "ok") |
| 35 | +}) |
| 36 | + |
| 37 | +test_that("otel instrumentation works with describe/it", { |
| 38 | + skip_if_not_installed("otelsdk") |
| 39 | + |
| 40 | + record <- with_otel_record({ |
| 41 | + with_reporter("silent", { |
| 42 | + describe("a feature", { |
| 43 | + it("passes", { |
| 44 | + expect_true(TRUE) |
| 45 | + }) |
| 46 | + it("fails", { |
| 47 | + expect_equal(1, 1) |
| 48 | + expect_true(FALSE) |
| 49 | + }) |
| 50 | + }) |
| 51 | + }) |
| 52 | + }) |
| 53 | + |
| 54 | + traces <- record$traces |
| 55 | + expect_length(traces, 3L) |
| 56 | + expect_equal(traces[[1L]]$name, "test that a feature / passes") |
| 57 | + expect_equal(traces[[1L]]$attributes[["test.expectations.total"]], 1) |
| 58 | + expect_equal(traces[[1L]]$attributes[["test.status"]], "pass") |
| 59 | + expect_equal(traces[[1L]]$status, "ok") |
| 60 | + expect_equal(traces[[2L]]$name, "test that a feature / fails") |
| 61 | + expect_equal(traces[[2L]]$attributes[["test.expectations.total"]], 2) |
| 62 | + expect_equal(traces[[2L]]$attributes[["test.expectations.passed"]], 1) |
| 63 | + expect_equal(traces[[2L]]$attributes[["test.expectations.failed"]], 1) |
| 64 | + expect_equal(traces[[2L]]$attributes[["test.status"]], "fail") |
| 65 | + expect_equal(traces[[2L]]$status, "error") |
| 66 | + expect_equal(traces[[3L]]$name, "test that a feature") |
| 67 | + expect_equal(traces[[3L]]$attributes[["test.expectations.total"]], 0) |
| 68 | +}) |
| 69 | + |
| 70 | +test_that("otel instrumentation works with nested test_that", { |
| 71 | + skip_if_not_installed("otelsdk") |
| 72 | + |
| 73 | + record <- with_otel_record({ |
| 74 | + with_reporter("silent", { |
| 75 | + test_that("outer test", { |
| 76 | + expect_true(TRUE) |
| 77 | + test_that("inner test fails", { |
| 78 | + expect_equal(1, 2) |
| 79 | + }) |
| 80 | + }) |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + traces <- record$traces |
| 85 | + expect_length(traces, 2L) |
| 86 | + expect_equal(traces[[1L]]$name, "test that outer test / inner test fails") |
| 87 | + expect_equal(traces[[1L]]$attributes[["test.expectations.total"]], 1) |
| 88 | + expect_equal(traces[[1L]]$attributes[["test.expectations.failed"]], 1) |
| 89 | + expect_equal(traces[[1L]]$attributes[["test.status"]], "fail") |
| 90 | + expect_equal(traces[[1L]]$status, "error") |
| 91 | + expect_equal(traces[[2L]]$name, "test that outer test") |
| 92 | + expect_equal(traces[[2L]]$attributes[["test.expectations.total"]], 1) |
| 93 | + expect_equal(traces[[2L]]$attributes[["test.status"]], "pass") |
| 94 | + expect_equal(traces[[2L]]$status, "ok") |
| 95 | +}) |
0 commit comments