Narrative
An immensely simple library for story-driven development
Overview
Narrative is a library dedicated to developing a whole or some part of software based on stories expressed in a Rust trait. Though its primary design is for end-to-end testing, its simplicity supports a variety of use cases.
Goals
- Story-driven: Code respects story, not the other way around
- Data-driven: Enabling stories to include structured data
- No additional tooling: Eliminating the need for extra installation and learning
- Leverage existing ecosystem: Rich experience with less implementation
- Zero runtime cost: Stories are processed at compile time
Terminology
Key terms in this library are:
- Story: a sequence of steps, written as a trait
- Step: a single action or assertion in a story
- Story Trait: a macro-generated trait that represents a story, with a method for each step
- Story Context: a struct that holds metadata about a story and provides methods to run it
- Story Env: a struct that implements a story trait
Usage
-
Add narrative to your cargo dependencies.
-
Write your first story as a trait.
Wow, it's neat!
- Implement the story in Rust.
You may notice that the signature of the trait methods is a bit different from the declaration, but it's fine.
- Use the story in your code.
use RunStory;
The #[narrative::story] macro generates a MyFirstStoryContext struct that
implements StoryContext, which provides methods to run the story and introspect
its structure.
Features
Async support
Both sync and async traits are defined automatically. Prefix the trait name with
Async to implement the async version.
Constants
Define constants in the story trait to use in step text and arguments.
Custom data types
Use #[narrative::local_type_for] to define custom types for step arguments.
This keeps stories independent from implementation details while leveraging Rust's
type system.
;
Types marked with #[narrative::local_type_for] can only be used in the specified
story, preventing coupling. Standard library types and common third-party types
(with serde::Serialize) like uuid::Uuid, chrono::DateTime, etc., can be used
directly without this attribute.
Sub stories
Stories can be composed by nesting them as steps. The parent step returns the sub story implementation.
Custom runners
Implement StoryRunner or AsyncStoryRunner to customize story execution,
add logging, reporting, or other cross-cutting concerns.
use StoryRunner;
;
Subtle but Important Points
Implementation details are omitted in story definitions
Stories should not be concerned with their implementation, so details like async,
&mut self, and -> Result<(), Self::Error> are not required in the trait
definition. The macro infers these from your implementation. Use rust-analyzer's
"Implement missing members" feature to generate the correct signatures.
Design Decisions
These decisions highlight Narrative's unique aspects, especially in comparison to Gauge, a well-known end-to-end testing framework.
Narrative supports multi-language step implementations
Stories can be introspected at runtime, allowing step implementations in other languages. The story context provides all metadata needed to dispatch steps to external processes:
use StoryContext;
Narrative is a library, not a framework
Narrative has no test runner, no plugin system, nor no dedicated language server. Instead of being a framework, Narrative is a library that provides just a single macro to implement stories. It's just a small tie between a story to a plain Rust code. So, users can compose their own test runners or async runtime with stories, and can use the full of rust-analyzer's functionality.
Narrative itself doesn't provide any features other than the core functionality, declaring stories as traits and implementing them in Rust code. It lays the groundwork for the simplicity and extensibility of this library.
The followings are the missing features in Narrative, and they never be implemented in this library. But don't forget that you can do them by leveraging the core features.
- step grouping
- story grouping
- test preparation and cleanup
- table-driven tests
- tags
- screenshoting
- retrying
- parallelization
- error reporting
Narrative uses a declaration of trait to write a story
In other words, a story is an interface and step implementation depends on it.
Gauge uses markdown, and it's a great format for writing specifications, documents, and stories while readable by non programmer. But, it's not the best format for expressing data in structured way. We think story is more like a data than a document, and it should be expressed in a structured way. With structured data, we can leverage the power of software in the processing of them. In Narrative, we use traits for expressing stories.
Using markdown for stories has another benefit, that is, it avoids the tight coupling between stories and the implementation. If stories depends on specific implementation, the story is not pure, and we loose many benefits of story-driven development. One of the benefits is that we, including non-programmer, can write stories freely without regard to the implementation, and it gives us a kind of agility to the development.
But, it's not the case in Narrative though it let you write stories in Rust. In Narrative, stories are written as traits, and it has no dependency to the implementation, and it's just a contract between the story and the implementation. Narrative would not loose the benefits of using markdown, on the contray, it would make the situation better.
Narrative explicitly separates the story and the implementation, and it forces the direction of the dependency. With markdown, we know that a story is the core of the development, but occasionally we forget it or have a kind of cognitive dissonance. It appeared to us as obvious experiences in the development, like, "we need to know defined tags in the implementation to write a correct story", "we have errors on the story editor if no step implementation", or "we failed to write the correct story because the steps chosen from the editor's suggestion are not implemented as we expect". In narrative, anyone can write stories anytime, and stories written can exist as valid real properties with no error even if implementation are completely undone.
The concept, a story is a contract to the implementation, makes the development process and logical dependency graph clean and simple, and although it requires a bit more effort to implement stories, it would give us a lot of benefits in the long run of the development.
Someone might think that writing or reading Rust traits is impossible or impractical to non-programmer, but we think it more optimistically. We are in the era of many people can read and write code with the help of the great tools and AIs, and, Personally, I believes clear codes wins documentation both for programmers and non-programmers, and I don't think non-programmers cannot read and write codes.
Narrative encourages no reusing of steps
We encourage you to write your stories in fresh mind every time without reusing existing steps, because we think stories should be self-contained. Being the situation comes with big wins described below.
Accessibility for Newcomers
It empowers story writers that are not familiar with the existing codebase. They don't need to know what steps already exist, to struggle with what steps to use, and to worry about whether the chosen step is implemented as they expect.
Contextual Clarity
Copying steps from other stories often leads to a mix-up of contexts, and making it not easy to decipher the key point of a story (without attaching proper aliases to common steps). While we tend to have many story have the same steps that shares the same context and implementation, it's challenging to maintain the coherency of sharing the same logic while we add, remove, modify the stories.
One downside of this approach is that stories could have inconsistency in the writing style among them, but it can be mitigated by organizing stories in the near each other with have the same contexts. It nudges writers to write stories in a consistent way.
Simplicity
Reusing steps or group of steps could be a source of complexity. It's nightmare to modify a step that is used by many stories without breaking them.
Fine-Grained Abstraction
A step is relatively large a unit for reuse or abstraction. Instead of sharing
the whole a step, we should share code between stories. But it should be done by
extracting common, story-agnostic, and atomic unit of logic. A step
implementation should be a composition of such units, and it should not leak the
story's context in the abstraction. For instance, if a step is about clicking a
submit button, it might be implemented as a composition of atomic logic like
find_element_by(id), click(element), and wait_for_page_load(), and not to
leak the context like click_submit_button() or click_button("#submit").