# Implementing testplan Setup and Teardown

**URL:** <https://forum.opentap.io/t/implementing-testplan-setup-and-teardown/790>\
**Category:** Technical\
**Created:** [August 2, 2022, 8:44am UTC](https://forum.opentap.io/t/implementing-testplan-setup-and-teardown/790 "2022-08-02T08:44:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![connospp](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@connospp](https://forum.opentap.io/u/connospp)\
**Post date:** [August 2, 2022, 8:44am UTC](https://forum.opentap.io/t/implementing-testplan-setup-and-teardown/790/1 "2022-08-02T08:44:14Z")

</div>

Hello again,

I am looking to implement Setup and Tear-down of a test plan which is something very common in all kinds of testing.  
Setup and tear-down are essentially parts of the testplan that do not affect the results. In the case of tear-down, it must be run whether the test was aborted, terminated by error or finished as pass or fail.

In my case i want to be setting up and turning off the Power supply. At the beginning and end of testplan

I was surprised to see that Opentap doesn’t support that and I am trying to think of a way to implement that myself.

I tried creating a teststep with override method Run empty and implement the pre-plan /post-plan methods that will execute child steps only there.

However that is not working, I believe opentap is only looking to execute the post and pre plan of the child steps since they are called from that point. And since those methods are not overridden in child steps they just throw a null exemption.

i know that each step has its own pre and post-plan that you can individually execute setup and tear-down but this is a different concept of what i am looking to implement.

---

<div class="post-metadata">

**Author:** ![alexander.larsen](https://avatars.discourse-cdn.com/v4/letter/a/9d8465/32.png) [@alexander.larsen](https://forum.opentap.io/u/alexander.larsen)\
**Post date:** [August 2, 2022, 1:19pm UTC](https://forum.opentap.io/t/implementing-testplan-setup-and-teardown/790/2 "2022-08-02T13:19:36Z")

</div>

Hi connospp,

It sounds like OpenTAP already supports your use-case. What you need to to is to implement an `Instrument` for your power supply. OpenTAP will call the `Open` method on any configured instrument (or resource) before the test plan starts, and `Close` after the test plan stops (whether it was aborted or completed normally).

I don’t think you actually need `SetUp / TearDown` in this instance, but it can be achieved by implementing the `ITestPlanRunMonitor` interface. The methods `EnterTestPlanRun` and `ExitTestPlanRun` will be executed immediately before and after a test plan. I would however advise against it because OpenTAP knows how to handle `IResource / IInstrument` implementations properly.

For example if you implement

```csharp
public class MyPowerSupply : Instrument {
  // implementation
}
...
public class MyTestStep : TestStep {
  public MyPowerSupply PSU { get; set; }
}

```

then the test plan GUI will let you easily choose between all configured instances of `MyPowerSupply` in your current bench settings, whereas you would need to create a hardcoded solution for your particular instrument in both the `PrePlanRun / PostPlanRun` and `ITestPlanRunMonitor` scenarios.

I hope this makes sense. If not, there are some example implementations of instruments and DUTs in the [SDK Examples](https://github.com/opentap/opentap/tree/main/sdk/Examples/PluginDevelopment/InstrumentsAndDuts)

---

<div class="post-metadata">

**Author:** ![connospp](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@connospp](https://forum.opentap.io/u/connospp)\
**Post date:** [August 2, 2022, 2:13pm UTC](https://forum.opentap.io/t/implementing-testplan-setup-and-teardown/790/3 "2022-08-02T14:13:29Z")

</div>

Thank you @alexander.larsen

Will look into the solution. Seems that it is what I look for
