Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assertions that compare the result with a file #822

Open
solita-cristian opened this issue Nov 6, 2024 · 0 comments
Open

Add assertions that compare the result with a file #822

solita-cristian opened this issue Nov 6, 2024 · 0 comments

Comments

@solita-cristian
Copy link

solita-cristian commented Nov 6, 2024

I noticed that the existing assertions work only with given values or variables, but not with files.

In my day job we would like to have the option to run assertions against golden files (files which contain the expected output of some operation).

For example, if we want to compare some JSON output, currently we do:

assertions:
    - result.bodyjson MustNotBeEmpty
    - result.bodyjson.somekey ShouldEqual ".."

I have two proposed solutions for this problem:

  1. Have File assertions, like the currently existing JSON assertions

This is the "hacky" solution as this would be essentially calling the existing assertions, with the second parameter given after reading a file, like:

func ShouldJSONFileEqual(actual interface{}, expectedFile interface{}) error {
  expected, err := // get bytes from file and dump
  return ShouldJSONEqual(actual, expected) error
} 
  1. Add a variable helper that reads from a file

This is the solution I like the most, as it would not add more similar assertions and it is a bit more "elegant" to look at when reading the test file.

If for example we have a file called ./expected.json

{
  "somekey": "somevalue"
}

We could test the result against it in this way:

vars:
    - expectedJsonFile: "./expected.json"
...
assertions:
    - result.bodyjson ShouldJSONEqual "{{ .expectedJsonFile | readFile }}"

This implementation solutions of course would not be limited to JSON, this is the most familiar example I came up with :)

Let me know what you think about this and if it is OK for you to continue with either implementation, or some other one, I can go ahead and start working on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant