# How to use agent-rs and replica in end2end test?

**URL:** <https://forum.dfinity.org/t/how-to-use-agent-rs-and-replica-in-end2end-test/22268>\
**Category:** Developers\
**Tags:** rust\
**Created:** [August 19, 2023, 4:13pm UTC](https://forum.dfinity.org/t/how-to-use-agent-rs-and-replica-in-end2end-test/22268 "2023-08-19T16:13:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![famouscat8](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/famouscat8/32/4732_2.png) [@famouscat8](https://forum.dfinity.org/u/famouscat8)\
**Post date:** [August 19, 2023, 4:13pm UTC](https://forum.dfinity.org/t/how-to-use-agent-rs-and-replica-in-end2end-test/22268/1 "2023-08-19T16:13:53Z")

</div>

How to use agent-rs and replica in end2end test?

---

<div class="post-metadata">

**Author:** ![NathanosDev](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/nathanosdev/32/7204_2.png) [@NathanosDev](https://forum.dfinity.org/u/NathanosDev)\
**Post date:** [August 19, 2023, 9:05pm UTC](https://forum.dfinity.org/t/how-to-use-agent-rs-and-replica-in-end2end-test/22268/2 "2023-08-19T21:05:19Z")

</div>

I’m not sure what problem you’re having, but you can see an example of e2e tests with agent-rs and replica (via DFX) here: [https://github.com/dfinity/response-verification/blob/main/packages/ic-response-verification-tests/src/main.rs](https://github.com/dfinity/response-verification/blob/main/packages/ic-response-verification-tests/src/main.rs)

You can get the address of the replica in a shell script like this:

```shell
DFX_REPLICA_PORT=$(dfx info replica-port)
DFX_REPLICA_ADDRESS="http://localhost:$DFX_REPLICA_PORT"

```

Then this `DFX_REPLICA_ADDRESS` variable is passed into the e2e test binary and it’s given to the agent:

```rust
let transport = ReqwestHttpReplicaV2Transport::create(env::var("DFX_REPLICA_ADDRESS").unwrap())?;

let agent = Agent::builder().with_transport(transport).build()?;
agent.fetch_root_key().await?;

```

---

<div class="post-metadata">

**Author:** ![famouscat8](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/famouscat8/32/4732_2.png) [@famouscat8](https://forum.dfinity.org/u/famouscat8)\
**Post date:** [August 19, 2023, 9:14pm UTC](https://forum.dfinity.org/t/how-to-use-agent-rs-and-replica-in-end2end-test/22268/3 "2023-08-19T21:14:22Z")

</div>

I have a multi-canister project that uses a lot of inter-canister calls. I want test those calls in the project level with product paths (using agent-rs to test). So i have to run a mainnet like environment in almost every tests, and then install the canister, call the method with arguments and maybe cycles, test the outputs. In this situation I can also test my project communicating with ledgers, cmc-canister…

So actually i need/want to write a test tool to start a totally fresh replica in every tests starting.

---

<div class="post-metadata">

**Author:** ![famouscat8](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/famouscat8/32/4732_2.png) [@famouscat8](https://forum.dfinity.org/u/famouscat8)\
**Post date:** [August 19, 2023, 9:19pm UTC](https://forum.dfinity.org/t/how-to-use-agent-rs-and-replica-in-end2end-test/22268/4 "2023-08-19T21:19:55Z")

</div>

> [@famouscat8](#):
>
> So actually i need/want to write a test tool to start a totally fresh replica in every tests starting.

dfx do similar works to start a replica. But it has some problems such as slow starting time, per project per replica state, communicating it through only command line…thus no suitable in my situation.

---

<div class="post-metadata">

**Author:** ![famouscat8](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/famouscat8/32/4732_2.png) [@famouscat8](https://forum.dfinity.org/u/famouscat8)\
**Post date:** [August 19, 2023, 9:24pm UTC](https://forum.dfinity.org/t/how-to-use-agent-rs-and-replica-in-end2end-test/22268/5 "2023-08-19T21:24:33Z")

</div>

And ic-test-state-machine lacks a lot of features of ic replica.

---

<div class="post-metadata">

**Author:** ![famouscat8](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/famouscat8/32/4732_2.png) [@famouscat8](https://forum.dfinity.org/u/famouscat8)\
**Post date:** [August 19, 2023, 9:35pm UTC](https://forum.dfinity.org/t/how-to-use-agent-rs-and-replica-in-end2end-test/22268/6 "2023-08-19T21:35:56Z")

</div>

Similar product in ethereal network: [Hardhat Network | Ethereum development environment for professionals by Nomic Foundation](https://hardhat.org/hardhat-network/docs/overview)

Hardhat comes built-in with Hardhat Network, a local Ethereum network node designed for development. It allows you to deploy your contracts, run your tests and debug your code, all within the confines of your local machine.

Here is the introduction of harhad’s test suite:

> ## [# How does it work?](https://hardhat.org/hardhat-network/docs/overview#how-does-it-work?)
> 
> It runs as either an in-process or stand-alone daemon, servicing JSON-RPC and WebSocket requests.  
> By default, it mines a block with each transaction that it receives, in order and with no delay.  
> It’s backed by the `@ethereumjs/vm` EVM implementation, the same one used by ganache, Remix and Ethereum Studio.

---

<div class="post-metadata">

**Author:** ![domwoe](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/domwoe/32/5165_2.png) [@domwoe](https://forum.dfinity.org/u/domwoe)\
**Post date:** [August 19, 2023, 9:49pm UTC](https://forum.dfinity.org/t/how-to-use-agent-rs-and-replica-in-end2end-test/22268/7 "2023-08-19T21:49:38Z")

</div>

Hi @famouscat8

you might want to have a look at the LightIC project [GitHub - icopen/lightic](https://github.com/icopen/lightic) by @stopak.

---

<div class="post-metadata">

**Author:** ![famouscat8](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/famouscat8/32/4732_2.png) [@famouscat8](https://forum.dfinity.org/u/famouscat8)\
**Post date:** [August 19, 2023, 9:58pm UTC](https://forum.dfinity.org/t/how-to-use-agent-rs-and-replica-in-end2end-test/22268/8 "2023-08-19T21:58:37Z")

</div>

Thx for reply, I already noticed that project. But it still not suitable for me as

1. I want to test in rust as my project writing in rust mostly.
2. It lacks a lot of features of ic replica. i checked lightic’s code , found it just another ic-test-state-machine but in typescript.

---

<div class="post-metadata">

**Author:** ![NathanosDev](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/nathanosdev/32/7204_2.png) [@NathanosDev](https://forum.dfinity.org/u/NathanosDev)\
**Post date:** [August 20, 2023, 11:50am UTC](https://forum.dfinity.org/t/how-to-use-agent-rs-and-replica-in-end2end-test/22268/9 "2023-08-20T11:50:23Z")

</div>

> communicating it through only command line

You can run shell commands through Rust too, you may need to make some convenience methods in your code to wrap these commands but that’s trivial to implement.

> slow starting time

That’s true, but is it really a blocker to use DFX? The e2e tests will take a long time to run, but they will work.

> per project per replica state

Why do you need to run your project on multiple replicas? Inter-canister query calls are run on the same replica on mainnet anyway.

For inter-canister update calls, your code should not change if you are making calls to canisters on the same subnet or another subnet. So if you want to test with multiple subnets locally then you are not testing your own code anymore, you are then testing the code of the network. You only need to test that your code works with inter-canister call on the same replica, everything else is the responsibility of the network.

> I can also test my project communicating with ledgers, cmc-canister

The `dfx nns` extension can install all of those canisters for you.

> a totally fresh replica in every tests starting

Do you mean for every test in your test suite that you will start a new replica? I think this is not necessary. If you need fresh canister state then you can just reinstall your own canister. This will be much faster that starting a fresh replica for every test.

---

<div class="post-metadata">

**Author:** ![stopak](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/stopak/32/9896_2.png) [@stopak](https://forum.dfinity.org/u/stopak)\
**Post date:** [August 21, 2023, 11:58am UTC](https://forum.dfinity.org/t/how-to-use-agent-rs-and-replica-in-end2end-test/22268/10 "2023-08-21T11:58:44Z")

</div>

Thanks for checking out the project. What features do you miss? You can make inter-canister calls and deploy multiple canisters. The hardhat was actually an inspiration for lightic
