# How to handle abort from a Test Step?

**URL:** <https://forum.opentap.io/t/how-to-handle-abort-from-a-test-step/333>\
**Category:** Technical\
**Tags:** opentap-sdk, faq, ks8400-editor\
**Created:** [June 2, 2021, 6:52pm UTC](https://forum.opentap.io/t/how-to-handle-abort-from-a-test-step/333 "2021-06-02T18:52:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaushik.santhanam](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opentap.io/kaushik.santhanam/32/208_2.png) [@kaushik.santhanam](https://forum.opentap.io/u/kaushik.santhanam)\
**Post date:** [June 2, 2021, 6:52pm UTC](https://forum.opentap.io/t/how-to-handle-abort-from-a-test-step/333/1 "2021-06-02T18:52:37Z")

</div>

There are two different abort scenarios.

### Scenario 1 - Programatically abort Test Plan Execution from `TestStep`

If you want to programmatically abort a Test Plan execution from a `TestStep` you can call the `PlanRun.MainThread.Abort()` method.

### Scenario 2 - Handling User Abort requests from the GUI in a `TestStep`

If you have a step that takes a long time to execute, you can call the `OpenTap.TapThread.ThrowIfAborted()` method to check if an abort is requested during the execution of the step.

---

<div class="post-metadata">

**Author:** ![kaushik.santhanam](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opentap.io/kaushik.santhanam/32/208_2.png) [@kaushik.santhanam](https://forum.opentap.io/u/kaushik.santhanam)\
**Post date:** [June 3, 2021, 9:10pm UTC](https://forum.opentap.io/t/how-to-handle-abort-from-a-test-step/333/2 "2021-06-03T21:10:12Z")

</div>

## Correction to scenario 1:

The best way to programmatically abort a test plan from a test plan is to just throw an exception and allowing OpenTAP to handle the rest.

This will make sure that the Test Plan is stopped in a controlled manner.

@rolf_madsen @janus.faaborg

---

<div class="post-metadata">

**Author:** ![david-wsd](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opentap.io/david-wsd/32/204_2.png) [@david-wsd](https://forum.opentap.io/u/david-wsd)\
**Post date:** [June 3, 2021, 9:33pm UTC](https://forum.opentap.io/t/how-to-handle-abort-from-a-test-step/333/3 "2021-06-03T21:33:10Z")

</div>

In scenario 2, sometimes my lengthy operations take place in an isolated class that does not reference OpenTAP, so the isolated class can’t call OpenTap.TapThread.ThrowIfAborted().

There’s an easy solution, just pass in PlanRun.MainThread.AbortToken to the isolated class. This works fine since PlanRun.MainThread.AbortToken is a CancellationToken, a common .NET type. Then the isolated class can call cancelToken.ThrowIfCancellationRequested(), so it will stop execution if the user has requested an abort.

---

<div class="post-metadata">

**Author:** ![GoranJohnsson](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opentap.io/goranjohnsson/32/140_2.png) [@GoranJohnsson](https://forum.opentap.io/u/GoranJohnsson)\
**Post date:** [September 10, 2021, 9:14pm UTC](https://forum.opentap.io/t/how-to-handle-abort-from-a-test-step/333/4 "2021-09-10T21:14:21Z")

</div>

What is the best way to abort from Resource Open()? Say you have an Instrument Resource and it cannot be opened for some reason. Also throw an exception?

---

<div class="post-metadata">

**Author:** ![john.berlien](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@john.berlien](https://forum.opentap.io/u/john.berlien)\
**Post date:** [September 12, 2021, 3:01pm UTC](https://forum.opentap.io/t/how-to-handle-abort-from-a-test-step/333/5 "2021-09-12T15:01:09Z")

</div>

Yes, then it will know that it failed to open.

---

<div class="post-metadata">

**Author:** ![david-wsd](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opentap.io/david-wsd/32/204_2.png) [@david-wsd](https://forum.opentap.io/u/david-wsd)\
**Post date:** [November 11, 2021, 6:22pm UTC](https://forum.opentap.io/t/how-to-handle-abort-from-a-test-step/333/6 "2021-11-11T18:22:51Z")

</div>

@kaushik.santhanam Regarding your correction to scenario 1, I think it depends on which verdict is preferred. If I throw an exception, the verdict is Error. If I execute PlanRun.MainThread.Abort(), the verdict is Abort.

---

<div class="post-metadata">

**Author:** ![john.berlien](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@john.berlien](https://forum.opentap.io/u/john.berlien)\
**Post date:** [November 12, 2021, 2:39pm UTC](https://forum.opentap.io/t/how-to-handle-abort-from-a-test-step/333/7 "2021-11-12T14:39:05Z")

</div>

I agree, if it’s an intentional abort then I use the Abort function, but if it’s an error that would cause the test plan to fail then I let it throw an exception
