lunarmodules / busted

Elegant Lua unit testing.
https://lunarmodules.github.io/busted/
MIT License
1.39k stars 184 forks source link

Variable captures #679

Open ii14 opened 2 years ago

ii14 commented 2 years ago

An alternative solution to #589 is to have a way of capturing variables that can be printed on assertion failures, to give them more context.

describe('foo', function()
  it('does something', function()
    for k, v in pairs({
      foo = 'foo',
      bar = 'foo',
    }) do
      capture.key = k
      assert.is_equal(k, v)
    end
  end)
end)

capture could be just a normal table that is reset before every test. And then on a failed assertion the table could be printed:

foo.lua:8: Expected objects to be equal.
Captures:
key = (string) 'bar'
Passed in:
(string) 'foo'
Expected:
(string) 'bar'