bicep icon indicating copy to clipboard operation
bicep copied to clipboard

Bicep Experimental Test Framework

Open sydkar opened this issue 2 years ago • 10 comments

Bicep Experimental Test Framework

Owner: Emily Redmond

Introduction

We began prototyping experimental test features early in summer 2023, and we’ve been encouraged by the level of customer interest in the project. While we are still early in exploring what the future of testing looks like in Bicep, we are eager to get early feedback. If you choose to enable the feature, beware the functionality is still experimental and subject to change at any time.

Read the full proposal for the testing framework here.

How to get started with experimental test framework

  1. Enable assertions and testFramework experimental feature flags in a bicepconfig.json file. Both feature flags must be enabled for expected functionality.
  2. Create a new .bicep file to author your tests, eg. tests.bicep. This must be a separate file from the .bicep template you want to test. Author a test block with the new test keyword, referencing a .bicep template you would like to test on the client-side (without deploying).
  3. Author assert statements to validate parameters, variables, and resource names within the .bicep template file you would like to test that you referenced in your test blocks.
  4. In your CLI, run the command bicep test <filepath_to_test_file> to run test blocks and their corresponding assert statements without deploying.

1. Enable experimental features

Enable testing experimental features in bicepconfig.json

  1. Create bicepconfig.json file within your Bicep project repo

image

  1. Within bicepconfig.json, define experimentalFeaturesEnabled and set testFramework: true and assertions: true

image

2. Test blocks

test block syntax:

  • test keyword
  • Name of test block (eg. testMain)
  • Reference to Bicep file to test (eg. ‘main.bicep’)
  • params object mocking parameter values from the Bicep file (eg. params: {env: 'prod'})

Example:

image

Lmitations:

  • You must pass in a parameter object, you cannot reference an existing parameter file currently
  • Test blocks must be in a separate file from the .bicep template you want to test
  • Test result output summary is not standardized

3. Assert statements

assert statement syntax:

  • assert keyword
  • Name of assert statement (eg. appName)
  • equals boolean validation expression (eg. = contains(appServiceApp.name, "${env}")

assert statements can be any boolean expression that references parameters, variables, or resource names. These boolean expressions can include Bicep functions such as "contains()", "length()", etc.

Examples:

image

Limitations:

  • You can only assert on parameters, variables, and resource names with current functionality
    • You cannot assert on resource size, location, or other propreties yet as these depend on runtime deployment functionality

sydkar avatar Sep 27 '23 16:09 sydkar

what version of bicep cli do I need for the test command?

And the asserts would be more useful if they could be chained from the object labeled "test". Filling them into my existing bicep files isn't easy.

dciborow avatar Nov 27 '23 06:11 dciborow

Are there any updates on this feature?

oliverlabs avatar Jan 18 '24 15:01 oliverlabs

@oliverlabs This project has been put on hold for the moment unfortunately. We will update this issue when we pick it back up!

stephaniezyen avatar Jan 26 '24 21:01 stephaniezyen

any updates?

aslan-im avatar Feb 21 '24 10:02 aslan-im

Any updates? This would be a great feature to have!

allux2 avatar Apr 23 '24 08:04 allux2

Hi @stephaniezyen , is there any update on this exciting feature, any plans to release it?

ElYusubov avatar Aug 07 '24 17:08 ElYusubov

Hi Folks - no updates. It's something we are eager to put more energy into, but do not have the ability to prioritize it at the moment.

alex-frankel avatar Aug 26 '24 22:08 alex-frankel