|
| 1 | + |
| 2 | +# Advanced Features in zio-bdd |
| 3 | + |
| 4 | +This page delves into advanced capabilities of `zio-bdd`, including integrating ZIO services, managing complex state, using `ScenarioContext` effectively, and leveraging custom reporters. These features enable you to write sophisticated tests for complex applications. |
| 5 | + |
| 6 | +## Integrating ZIO Services |
| 7 | + |
| 8 | +`zio-bdd` seamlessly integrates with ZIO's dependency injection system via `ZLayer`. This allows you to provide services to your step definitions, making your tests modular and easy to manage. |
| 9 | + |
| 10 | +### Providing Services |
| 11 | + |
| 12 | +In your `ZIOSteps` subclass, override the `environment` method to provide the required services: |
| 13 | + |
| 14 | +```scala |
| 15 | +override def environment: ZLayer[Any, Any, Env] = |
| 16 | + ZLayer.make[Env]( |
| 17 | + Service1.live, |
| 18 | + Service2.live |
| 19 | + ) |
| 20 | +``` |
| 21 | + |
| 22 | +- **Env**: The type of the environment required by your steps (e.g., `Service1 with Service2`). |
| 23 | + |
| 24 | +### Using Services in Steps |
| 25 | + |
| 26 | +Access services in your step definitions using `ZIO.service`: |
| 27 | + |
| 28 | +```scala |
| 29 | +When("some action is performed") { |
| 30 | + for { |
| 31 | + service <- ZIO.service[Service1] |
| 32 | + result <- service.doSomething() |
| 33 | + // ... |
| 34 | + } yield () |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +This approach keeps your tests clean and decoupled from service implementations. |
| 39 | + |
| 40 | +## Managing Complex State |
| 41 | + |
| 42 | +For tests requiring intricate state management, `ScenarioContext` can hold complex data structures. Define a case class to represent your state and provide an implicit `Schema` for serialization. |
| 43 | + |
| 44 | +### Example |
| 45 | + |
| 46 | +```scala |
| 47 | +case class ComplexState(users: Map[String, User], currentUser: Option[String]) |
| 48 | +implicit val schema: Schema[ComplexState] = DeriveSchema.gen[ComplexState] |
| 49 | + |
| 50 | +Given("a user named " / string) { (name: String) => |
| 51 | + for { |
| 52 | + state <- ScenarioContext.get |
| 53 | + updatedUsers = state.users + (name -> User(name)) |
| 54 | + _ <- ScenarioContext.update(_.copy(users = updatedUsers)) |
| 55 | + } yield () |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +- Use `ScenarioContext.update` to modify the state immutably. |
| 60 | + |
| 61 | +## Custom Reporters |
| 62 | + |
| 63 | +While `zio-bdd` provides built-in reporters, you can create custom reporters for specialized output formats. |
| 64 | + |
| 65 | +### Implementing a Custom Reporter |
| 66 | + |
| 67 | +Extend the `Reporter` trait: |
| 68 | + |
| 69 | +```scala |
| 70 | +class CustomReporter extends Reporter { |
| 71 | + override def report(results: List[FeatureResult]): ZIO[Any, Nothing, Unit] = { |
| 72 | + // Custom reporting logic |
| 73 | + ZIO.succeed(()) |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +- Register your reporter in the `@Suite` annotation: |
| 79 | + |
| 80 | +```scala |
| 81 | +@Suite(reporters = Array("custom")) |
| 82 | +object MySpec extends ZIOSteps[Env, State] { |
| 83 | + // ... |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +- You'll need to map `"custom"` to your `CustomReporter` instance in the test framework configuration. |
| 88 | + |
| 89 | +## Scenario Outlines and Examples |
| 90 | + |
| 91 | +`zio-bdd` fully supports Gherkin’s `Scenario Outline` and `Examples` for data-driven testing. |
| 92 | + |
| 93 | +### Defining Steps for Outlines |
| 94 | + |
| 95 | +Write step definitions as usual, using extractors for placeholders: |
| 96 | + |
| 97 | +```scala |
| 98 | +Given("a user named " / string) { (name: String) => |
| 99 | + // ... |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +- `zio-bdd` automatically handles the expansion of outlines into multiple scenarios. |
| 104 | + |
| 105 | +## Background Steps |
| 106 | + |
| 107 | +Gherkin’s `Background` section runs before each scenario in a feature. Define steps for background as you would for any other step. |
| 108 | + |
| 109 | +### Example |
| 110 | + |
| 111 | +In your feature file: |
| 112 | + |
| 113 | +```gherkin |
| 114 | +Feature: User Management |
| 115 | + Background: |
| 116 | + Given a system is running |
| 117 | + Scenario: Create user |
| 118 | + When a user is created |
| 119 | + Then the user exists |
| 120 | +``` |
| 121 | + |
| 122 | +Define the background step: |
| 123 | + |
| 124 | +```scala |
| 125 | +Given("a system is running") { |
| 126 | + // Setup system |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +- Background steps are executed before each scenario, ensuring a consistent starting state. |
| 131 | + |
| 132 | +## Best Practices |
| 133 | + |
| 134 | +- **Service Mocking**: Use test doubles or mocks for services to isolate tests. |
| 135 | +- **State Reset**: Ensure each scenario starts with a clean state, especially when using shared resources. |
| 136 | +- **Custom Extractors**: For complex parameter types, define custom extractors extending `TypedExtractor`. |
| 137 | + |
| 138 | +## Next Steps |
| 139 | + |
| 140 | +- Explore practical applications in [Examples](examples.md). |
| 141 | +- Contribute to `zio-bdd` by checking the [Contribution Guide](contributing.md). |
0 commit comments