Will teardown() be executed even after a default function has failed?

Use case:
export default function() {
multiple http calls.
if any singe http call fails, stop executing the rest of the code with check and fail statement.
}
export function teardown(){
some code
}

My question is: will teardown() be executed even after default function is aborted? In my case, teardown is always needed at the end of test execution, even after the test aborts in the middle of a process. Cucumber test framework has such handy feature, but I tried with K6, it looks like it is not able to do that.
Thanks.

Hi @minwu,

the teardown function is always executed, regardless of the test result.

To abort the default function, you on any http failure, you can create a custom metric, let’s say failedRequestCounter and create a threshold with abortOnFail parameter.

See example here:

import http from 'k6/http'
import { Counter } from 'k6/metrics'
import { sleep, check as loadTestingCheck } from "k6";

let failedRequestCounter = new Counter('failedRequestCounter');

export let options = {
  thresholds: {
	  failedRequestCounter: [{ threshold: 'count==0', abortOnFail: true }], // abort the test on any failed requests
  },
  vus: 1,  
  duration: '30s',
}

let check = function (obj, conditionArray, tags) {
  let result = loadTestingCheck(obj, conditionArray, tags || {});
  failedRequestCounter.add(!result);
  return result;
}

export default function () {
  let res = http.get('https://test-api.loadimpact.com/public/crocodiles/')

  check(res.status, {
    "API is working": (status) => status === 200
  });

  res = http.get('https://test-api.loadimpact.com/public/crocodiles/a') // 404

  check(res.status, {
    "some other check": (status) => status === 201
  });

 sleep(1);
}

export function teardown(){
 console.log("terdown executed");
}

The result of running this test is:

@pawel,
Thanks for the sample code. I will try it and let you know the results

@pawel, I got a different error with your code and the teardown() is not called. Is it something wrong with my configuration?

$ k6 run debug.js

          /\      |‾‾|  /‾‾/  /‾/   
     /\  /  \     |  |_/  /  / /    
    /  \/    \    |      |  /  ‾‾\  
   /          \   |  |‾\  \ | (_) | 
  / __________ \  |__|  \__\ \___/ .io

  execution: local
     output: -
     script: debug.js

    duration: 1m0s, iterations: 1
         vus: 1,    max: 1

    done [==========================================================] 1.9s / 1m0s
WARN[0002] No data generated, because no script iterations finished, consider making the test duration longer 

    ✓ API is working
    ✗ some other check
     ↳  0% — ✓ 0 / ✗ 1

    checks.....................: 50.00% ✓ 1   ✗ 1  
    data_received..............: 0 B    0 B/s
    data_sent..................: 0 B    0 B/s
  ✗ failedRequestCounter.......: 1      0.50422/s
    http_req_blocked...........: avg=450.8ms  min=1µs      med=450.8ms  max=901.6ms  p(90)=811.44ms p(95)=856.52ms
    http_req_connecting........: avg=21.23ms  min=0s       med=21.23ms  max=42.46ms  p(90)=38.21ms  p(95)=40.33ms 
    http_req_duration..........: avg=145.13ms min=130.31ms med=145.13ms max=159.95ms p(90)=156.98ms p(95)=158.47ms
    http_req_receiving.........: avg=154µs    min=97µs     med=154µs    max=211µs    p(90)=199.6µs  p(95)=205.29µs
    http_req_sending...........: avg=134.5µs  min=59µs     med=134.5µs  max=210µs    p(90)=194.9µs  p(95)=202.45µs
    http_req_tls_handshaking...: avg=363.27ms min=0s       med=363.27ms max=726.55ms p(90)=653.9ms  p(95)=690.23ms
    http_req_waiting...........: avg=144.84ms min=130.04ms med=144.84ms max=159.64ms p(90)=156.68ms p(95)=158.16ms
    http_reqs..................: 2      1.008439/s
    vus........................: 1      min=1 max=1
    vus_max....................: 1      min=1 max=1

ERRO[0002] some thresholds have failed

Unfortunately, teardown() won’t always be called in the current k6 version, if the script gets aborted by threshold, or some other way. I recently fixed that behavior, among other things, in #1390, but that has only been merged in #1007, it hasn’t been merged in master or been officially released yet.

@ned, thanks for the information. I will check with newer k6 version.