Tests

Testing practices are key to guarantee the consistency of models. In this section, you will learn how to write tests for your model.

Basics

Consider a process with a complicated formula.

process wheat_production {
    params {
        length = 500 m
        area_usage = 0.95 u
        yield = 300 kg / 10000 m2
    }
    products {
        1 kg wheat
    }
    variables {
        area = length^2.0
        crop_area = area_usage * area
        ratio = yield / 1 kg
    }
    impacts {
        area / (ratio * crop_area ) LU
    }
}

Very often, in LCA, you already have an idea of the order of magnitude of an impact. For instance, here, one could quickly estimate that the LU impact's order of magnitude should be between 50 and 100 square meter.

Of course, you can check this assertion manually, by clicking on the and assessing the process. However, you can also explicitly write the test as follows

test landUseImpactShouldBeBetween10and100 {
    given {
        1 kg wheat from wheat_production
    }
    assert {
        LU between 50 m2 and 100 m2
    }
}

First, the test has a name landUseImpactShouldBeBetween10and100. You are free to name the test as you wish, but it is good practice to use a name that describes the intent. The block given describes the quantity of product you want to assess the impact for. Hence, in this example, you are given 1 kilogram of wheat and you want to assess its impacts. The block assert contains an assertion: this is a statement about the impact of the chosen product that you expect to be true. In this example, you expect the LU impact to lie between 50 and 100 square meters.

You can run the test by clicking on the icon next to the beginning of the test block. What is happening?

An image showing the result of landUseImpactShouldBeBetween10and100

The test is red, meaning that the assertion is false. Indeed, we see that the actual value is about 35. At this point, either the model is wrong, or the assertion is wrong. In either case, the test is here to give you a feedback on the mismatch what you expect and what is actually modeled. You can modify the test to make it pass.

test landUseImpactShouldBeBetween10and100 {
    given {
        1 kg wheat from wheat_production
    }
    assert {
        LU between 30 m2 and 100 m2
    }
}

img.png

Parameters

As you may have guessed, the syntax in the block given is the same as in the block inputs. In particular, you can fill in specific values for parameters. E.g.

test landUseImpactShouldBeBetween10and100_withParams {
    given {
        1 kg wheat from wheat_production(
                   length = 1000 m,
                   area_usage = 0.80 u,
                   yield = 500 kg / 10000 m2,
                   )
    }
    assert {
        LU between 30 m2 and 100 m2
    }
}

As an exercise, find a value for area_usage that makes the test pass.

A solution
test landUseImpactShouldBeBetween10and100_withParams {
    given {
        1 kg wheat from wheat_production(
                   length = 1000 m,
                   area_usage = 0.50 u,
                   yield = 500 kg / 10000 m2,
                   )
    }
    assert {
        LU between 30 m2 and 100 m2
    }
}

Assertion on a intermediary product

Consider the following processes

process bread_production {
    products {
        500 g bread
    }
    inputs {
        1 kg wheat from wheat_production
        1 kWh electricity
    }
}

process wheat_production {
    params {
        length = 500 m
        area_usage = 0.95 u
        yield = 300 kg / 10000 m2
    }
    products {
        1 kg wheat
    }
    variables {
        area = length^2.0
        crop_area = area_usage * area
        ratio = yield / 1 kg
    }
    inputs {
        crop_area * 1 kWh / ha electricity
    }
    impacts {
        area / (ratio * crop_area ) LU
    }
}

process electricity_production {
    products {
        1 kWh electricity
    }
    impacts {
        1 kg GWP
    }
}

You see that electricity is used in the two processes bread_production and wheat_production. It is not obvious how much electricity is used in the production of 1 kg of bread. To check that, one can also assert on the quantity of electricity in a test.

test electricityForBread {
    given {
        1 kg bread from bread_production
    }
    assert {
        electricity between 10 kWh and 100 kWh
    }
}

Complex demands and multiple assertions

One can ask for various products and write multiple assertions in the same test.

test electricityForBread_complex {
    given {
        1 kg bread from bread_production
        100 g wheat from wheat_production
    }
    assert {
        electricity between 10 kWh and 100 kWh
        GWP between 10 kg and 100 kg
        LU between 100 m2 and 1000 m2
    }
}

This test is to be interpreted as follows. In order to match the demand of

  • 1 kg of bread
  • and 100 g of wheat,

the system

  • supplies a quantity of electricity between 10 kWh and 100 kWh,
  • has a GWP impact between 10 kg and 100 kg,
  • and a LU impact between 100 m2 and 1000 m2.