<p>There are now test capabilities in the Plutus.Contract.Test module which uses the Tasty library.</p>
<p>This is explained more fully in <a href="https://plutus-pioneer-program.readthedocs.io/en/latest/week8.html#unit-testing" rel="nofollow noreferrer">the unit tests section of lecture 8 of iteration 1 of the Plutus Pioneer Program</a>.</p>
<p>Some highlights:</p>
<pre><code>checkPredicate :: String -&gt; TracePredicate -&gt; EmulatorTrace () -&gt; TestTree
</code></pre>
<p>There are a number of functions for producing a TracePredicate. For example:</p>
<pre><code>-- | Check that the funds in the wallet have changed by the given amount, exluding fees.
walletFundsChange :: Wallet -&gt; Value -&gt; TracePredicate
</code></pre>
<p>Which can then be combined into a test function:</p>
<pre><code>tests :: TestTree
tests = checkPredicateOptions
   (defaultCheckOptions &amp; emulatorConfig .~ emCfg)
&quot;token sale trace&quot;
   ( walletFundsChange (Wallet 1) (Ada.lovelaceValueOf   10_000_000  &lt;&gt; assetClassValue token (-60))
.&amp;&amp;. walletFundsChange (Wallet 2) (Ada.lovelaceValueOf (-20_000_000) &lt;&gt; assetClassValue token   20)
.&amp;&amp;. walletFundsChange (Wallet 3) (Ada.lovelaceValueOf (- 5_000_000) &lt;&gt; assetClassValue token    5)
   )
myTrace
</code></pre>
<p>Which can then be run like this:</p>
<pre><code>Prelude Spec.Trace&gt; import Test.Tasty
Prelude Test.Tasty Spec.Trace&gt; defaultMain tests
token sale trace: OK (1.22s)

All 1 tests passed (1.22s)
</code></pre>
<p>Naturally, these tests can be configured to run as part of &quot;cabal test&quot;.</p>

CC BY-SA 4.0
2021-07-08T21:42:40.290Z
2021-07-08T21:42:40.290Z
Chrismo
<p>You can use Plutus Playground to test your code and maybe in the near future we will have more options.</p>

CC BY-SA 4.0
2021-05-06T13:05:36.223Z
2021-05-07T06:16:00.297Z
2021-05-07T06:16:00.297Z
Abkade
<p>Is there infrastructure for writing unit/integration tests for our contracts, without running the full playground?  Is there a test-focused EvaluatorTrace monad, for example?</p>

<plutus><plutus-playground><monad>
Test-Focused EvaluatorTrace Monad?
# Test-Focused EvaluatorTrace Monad?

(Asked by: username_0 on 2021-05-06)

Is there infrastructure for writing unit/integration tests for our contracts, without running the full playground? Is there a test-focused EvaluatorTrace monad, for example?

**Tags:** plutus, plutus-playground, monad

---

## 2 answers

### Answer by username_1 on 2021-07-08. Score: 2

There are now test capabilities in the Plutus.Contract.Test module which uses the Tasty library.
This is explained more fully in the unit tests section of lecture 8 of iteration 1 of the Plutus Pioneer Program.
Some highlights:
checkPredicate :: String -> TracePredicate -> EmulatorTrace () -> TestTree

There are a number of functions for producing a TracePredicate. For example:
-- | Check that the funds in the wallet have changed by the given amount, exluding fees.
walletFundsChange :: Wallet -> Value -> TracePredicate

Which can then be combined into a test function:
tests :: TestTree
tests = checkPredicateOptions
   (defaultCheckOptions & emulatorConfig .~ emCfg)
"token sale trace"
   ( walletFundsChange (Wallet 1) (Ada.lovelaceValueOf   10_000_000  <> assetClassValue token (-60))
.&&. walletFundsChange (Wallet 2) (Ada.lovelaceValueOf (-20_000_000) <> assetClassValue token   20)
.&&. walletFundsChange (Wallet 3) (Ada.lovelaceValueOf (- 5_000_000) <> assetClassValue token    5)
   )
myTrace

Which can then be run like this:
Prelude Spec.Trace> import Test.Tasty
Prelude Test.Tasty Spec.Trace> defaultMain tests
token sale trace: OK (1.22s)

All 1 tests passed (1.22s)

Naturally, these tests can be configured to run as part of "cabal test".


---

### Answer by username_2 on 2021-05-06. Score: 0

You can use Plutus Playground to test your code and maybe in the near future we will have more options.


---

