Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To test smart contracts in Truffle, put JavaScript, TypeScript, or Solidity tests in ./test and run truffle test. Truffle can run them against Ganache or Truffle Develop, isolate suites, and report failed transactions. ConsenSys announced Truffle’s sunset on September 21, 2023, so it remains useful for understanding or maintaining existing projects, but new projects should evaluate maintained alternatives.

Run a Truffle test suite

Truffle’s testing framework accepts JavaScript and TypeScript tests as well as Solidity test contracts. Put test files in the project’s ./test directory. Run the full suite from the project root with:

truffle test

To run one test file, pass its path:

truffle test ./test/file.js

Replace the example path with the file you want to run. Truffle’s testing guide documents both commands and the supported test types.

Choose JavaScript or Solidity tests

Test type Best suited to How Truffle runs it
JavaScript or TypeScript Tests written in the JavaScript ecosystem, using Mocha test structure and Chai assertions. Use contract() to define a suite and artifacts.require() to obtain a contract abstraction. Truffle supplies accounts from the Ethereum client and redeploys contracts before each contract() suite. See the JavaScript testing guide.
Solidity Tests written as Solidity contracts, useful when keeping test logic in Solidity is a better fit for the project. Place .sol test contracts in ./test. Truffle runs them as separate suites, provides clean-room behavior and direct access to deployed contracts, and includes a default assertion library without requiring a special base contract. See the Solidity testing guide.

For JavaScript tests, contract() is similar to Mocha’s describe(), but adds Truffle-specific setup. Choose the language that suits the codebase and the people maintaining its tests; both approaches are supported by the legacy workflow.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Run tests with Ganache or another client

For routine local development, Truffle recommends Ganache or Truffle Develop because they are faster to use than testing against a full Ethereum client. The quickstart describes truffle test starting a local Ganache instance automatically when no development network is configured. You can also configure Truffle to use a Ganache instance that is already running in truffle-config.

The exact network behavior depends on your configuration: an automatically started local instance is different from selecting a separately running Ganache network. Check the project’s truffle-config when tests connect to an unexpected client or network.

Before production deployment, Truffle’s testing guide recommends running tests once against go-ethereum or another official Ethereum client, in addition to the faster development-client runs.

Understand test isolation

With Ganache or Truffle Develop, Truffle uses snapshots so test files do not share state. When tests run against other Ethereum clients, Truffle instead redeploys migrations at the start of each test file to provide a fresh environment. This isolation is at the test-file level; within a test file, account and contract state can still be affected by the tests you run.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Diagnose failed transactions

For Solidity stack traces on failed transactions, run:

truffle test --stacktrace

For additional revert messages, add --stacktrace-extra, which also enables Solidity debug mode:

Rank #4
Sale
Mastering Bitcoin: Programming the Open Blockchain
  • Brand New in box. The product ships with all relevant accessories
truffle test --stacktrace-extra

Truffle marks this diagnostic feature experimental. While it is enabled, calls and gas estimates are unsupported, and some PromiEvent behavior may not work. Treat it as a troubleshooting aid rather than a default test mode; the limitations are described in the testing guide.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What to use now that Truffle is sunset

ConsenSys announced Truffle’s sunset on September 21, 2023, as recorded on the Solidity documentation resources page. That makes Truffle most relevant today as a workflow for maintaining existing contracts and test suites, rather than a default choice for a new project.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a new project or a migration, compare the language your team wants to use, how tests isolate chain state, the local and official-client workflows you need, and the diagnostic features available. Ethereum.org’s smart-contract testing guide lists Hardhat, Foundry, Remix, Ape, and Wake among testing resources. It also notes that testing frameworks span JavaScript, Python, and Rust; evaluate the current documentation and fit of any candidate before choosing it.

For an existing Truffle project, first inventory its test files, network configuration, contract abstractions, migrations, and any reliance on Truffle-specific helpers. Then assess the migration work against the target framework’s way of running tests and connecting to local or official clients. The cited resources establish Truffle’s sunset and name alternatives, but do not prescribe a one-to-one migration path or establish which tool is best for every project.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.