# How do you write unit test for contracts?

**URL:** <https://forum.fuel.network/t/how-do-you-write-unit-test-for-contracts/5949>\
**Category:** Sway\
**Tags:** feedback, faqs, tutorials\
**Created:** [June 20, 2024, 9:05am UTC](https://forum.fuel.network/t/how-do-you-write-unit-test-for-contracts/5949 "2024-06-20T09:05:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eesheng](https://avatars.discourse-cdn.com/v4/letter/e/2bfe46/32.png) [@eesheng](https://forum.fuel.network/u/eesheng)\
**Post date:** [June 20, 2024, 9:05am UTC](https://forum.fuel.network/t/how-do-you-write-unit-test-for-contracts/5949/1 "2024-06-20T09:05:29Z")

</div>

According to this docs, it is a script and did not show which directory is it yet nor the imported files. How do you test the contract instance? Showing this code snippet doesn’t help us in elaborating how to unit tests from contract.

```auto
contract;
 
abi CallerContract {
    fn test_false() -> bool;
}
 
impl CallerContract for Contract {
    fn test_false() -> bool {
        false
    }
}
 
abi CalleeContract {
    fn test_true() -> bool;
}
 
#[test]
fn test_multi_contract_calls() {
    let caller = abi(CallerContract, CONTRACT_ID);
    let callee = abi(CalleeContract, callee::CONTRACT_ID);
 
    let should_be_false = caller.test_false();
    let should_be_true = callee.test_true();
    assert(!should_be_false);
    assert(should_be_true);
}

```

This script is completely unrelated from the contract above. Can we have a tutorial on how to test the contract above?

```auto
script;
 
fn main() {}
 
#[test]
fn test_fn() {
let a = 10;
    log(a);
    let b = 30;
    log(b);
    assert_eq(a, 10)
    assert_eq(b, 30)
}

```

> **[Sway | Fuel Docs](https://docs.fuel.network/docs/sway/testing/unit-testing/#logs-inside-tests)**
>
> Official documentation for the Sway language

---

<div class="post-metadata">

**Author:** ![crypt0mate](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/crypt0mate/32/2445_2.png) [@crypt0mate](https://forum.fuel.network/u/crypt0mate)\
**Post date:** [June 20, 2024, 7:16pm UTC](https://forum.fuel.network/t/how-do-you-write-unit-test-for-contracts/5949/2 "2024-06-20T19:16:40Z")

</div>

Hi, take a look at how the Sway-Store contract is tested in the Sway for Javascript Devs guide.

[Rust Testing | Fuel Docs](https://docs.fuel.network/guides/intro-to-sway/rust-sdk/)

if you have time, it might be worthwhile to go through the entire guide.  
[Intro to Sway | Fuel Docs](https://docs.fuel.network/guides/intro-to-sway/)

---

<div class="post-metadata">

**Author:** ![eesheng](https://avatars.discourse-cdn.com/v4/letter/e/2bfe46/32.png) [@eesheng](https://forum.fuel.network/u/eesheng)\
**Post date:** [June 21, 2024, 5:26am UTC](https://forum.fuel.network/t/how-do-you-write-unit-test-for-contracts/5949/3 "2024-06-21T05:26:49Z")

</div>

Hi, do check out [Sway | Fuel Docs](https://docs.fuel.network/docs/sway/testing/unit-testing/) where you can write test in the same file for **unit test** without Rust. My problem is writing Unit Test not Integration test.

---

<div class="post-metadata">

**Author:** ![crypt0mate](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/crypt0mate/32/2445_2.png) [@crypt0mate](https://forum.fuel.network/u/crypt0mate)\
**Post date:** [June 21, 2024, 11:13am UTC](https://forum.fuel.network/t/how-do-you-write-unit-test-for-contracts/5949/4 "2024-06-21T11:13:47Z")

</div>

Sure thanks, if you look further down there is a unit test inside a contract is this not the example you are looking for ?

 ![image](https://us1.discourse-cdn.com/flex022/uploads/fuel/original/2X/5/59a01b49a52ad5c565af3100ba54e0aec1adb1a8.png)

 ![image](https://us1.discourse-cdn.com/flex022/uploads/fuel/original/2X/1/1b5dbfccaf9fe1b1dde26a8e6143b4479da78cf9.png)

---

<div class="post-metadata">

**Author:** ![eesheng](https://avatars.discourse-cdn.com/v4/letter/e/2bfe46/32.png) [@eesheng](https://forum.fuel.network/u/eesheng)\
**Post date:** [June 21, 2024, 12:27pm UTC](https://forum.fuel.network/t/how-do-you-write-unit-test-for-contracts/5949/5 "2024-06-21T12:27:00Z")

</div>

Hi, what I meant was using a script file to test a contract. Using script allows to check the logs.
