#Dynamically created test functions?

1 messages · Page 1 of 1 (latest)

woven herald
#

Can we dynamically generate the names of tests? I have a file which defines the tests in a JSON format. The file comes from an external source. I wanted to dynamically generate the names of tests so that the test reporter's output would be able to tell us which exactly failed. Is this possible without codegen?

thick walrus
#

you wouldnt be able to use gleeunit cause thats not what its designed for, but there's nothing stopping you from making your own test runner and calling that in your test module's main function instead of gleeunit

frigid ermine
#

Whats wrong with codegen?

woven herald
woven herald
thick walrus
#

yeah thats not in scope for gleeunit

woven herald
#

cool

#

Thank you then!

#

I don't have time to do that right now but would be a fun project eventually

thick walrus
#

theres also nothing special about gleeunit, just cause its added as a dependency when you make a new project doesnt mean you have to use it

#

gleam test isnt dependant on gleeunit in any way

woven herald
#

Good to know

#

was my first time using gleam test

thick walrus
#

but yeah i agree with hayleigh, i would use codegen for this

woven herald
#

an extra command to run before gleam test?

thick walrus
#

youd only need to run the codegen when the json changes

#

so not for every test

#

you could use gleam dev to run it

woven herald
woven herald
# thick walrus so not for every test

to me this feels like it should not be necessary, i would expect it to just work when doing gleam test. or at least that is the experience i desire

frigid ermine
#

its not an experience you can get

#

you could write a single test that loads the json and does whatever, in the assertion message include the name of the test or whatever

woven herald
frigid ermine
#

The your tests are dealing with json parsing and error handling

#

Why cant you write the tests in gleam?

woven herald
#

the json is very big/would be very time consuming

#

and is external/im not updating it

#

it is a library-agnostic test suite from the spec of a protocol

#

e.g. "consume this and make sure your implementation passes/fails this input with this output"

woven herald
#

Cool

#

yeah it's definitely like

#

"not broken"

#

just would be cool to show them separately, but no worries

frigid ermine
#

You dont need to use gleeunit as lily mentions, so you’re not constrained by the “tests must be named _test” convention.

Really youd write a gleam program to load the JSON and does assertions as you currently are just make sure the assertion messages are good

#

In case you didnt know already you can do `as …’ to show a specific message

#
assert thing(input) == expected as test_name
``` or something
woven herald
#

Yeah super useful, I am using it now

#

Cool thank you both for the help 🙂

winged crescent
#

That’s how the tests for my toml parser library works. It loads the tests copied from the official parser JS library and iterates over them