Search code examples
unit-testingcrystal-lang

Crystal automated unit tests at compilation


In my understanding, Crystal can perform Unit tests while compiling but I could not find much info on them.

Can Crystal perform Unit tests while compiling ? If yes how can I implement them in a new project and how do I give them arguments ?
Ex : if some tests require internet can I somehow send / give / set an argument to desactivate them if I have no internet ?


Solution

  • While not exactly an answer to your question, Guard may work for you. Here's how I have set it up, and works very well. Although not a Crystal shard, it's a Ruby gem that works great in dev environment.

    Whenever I save a file, guard runs all the specs (which would be your unit tests). I gather that all the code is compiled before running.

    Although Crystal Spec (https://crystal-lang.org/api/0.23.1/Spec.html) does not provide tagging, you can specify a folder: crystal spec spec/units. You can put the specs that require internet in another folder.

    Another approach you can use is to write a bash (or Ruby) script that compiles and runs the specs.

    BTW, you probably don't want to use the internet for you specs, unit or otherwise. You can mock them using Webmock, or by re-opening your classes in the test environment. I have been using the latter approach, and it works well.