Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert library documentation added #2458

Merged
merged 2 commits into from
Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions docs/assert_library.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
Remix Assert Library
====================

* [Assert.ok(value[, message])](#assert-ok-value-message)
* [Assert.equal(actual, expected[, message])](#assert-equal-actual-expected-message)
* [Assert.notEqual(actual, expected[, message])](#assert-notequal-actual-expected-message)
* [Assert.greaterThan(value1, value2[, message])](#assert-greaterthan-value1-value2-message)
* [Assert.lesserThan(value1, value2[, message])](#assert-lesserthan-value1-value2-message)


## Assert

### Assert.ok(value[, message])
* `value`: \<bool\>
* `message`: \<string\>

Tests if value is truthy. `message` is returned in case of failure.

Examples:
```
Assert.ok(true);
// OK
Assert.ok(false, "it\'s false");
// error: it's false
```

### Assert.equal(actual, expected[, message])
* `actual`: \<uint | int | bool | address | bytes32 | string\>
* `expected`: \<uint | int | bool | address | bytes32 | string\>
* `message`: \<string\>

Tests if `actual` & `expected` values are same. `message` is returned in case of failure.

Examples:
```
Assert.equal(string("a"), "a");
// OK
Assert.equal(uint(100), 100);
// OK
foo.set(200)
Assert.equal(foo.get(), 200);
// OK
Assert.equal(foo.get(), 100, "value should be 200");
// error: value should be 200
```

### Assert.notEqual(actual, expected[, message])
* `actual`: \<uint | int | bool | address | bytes32 | string\>
* `expected`: \<uint | int | bool | address | bytes32 | string\>
* `message`: \<string\>

Tests if `actual` & `expected` values are not same. `message` is returned in case of failure.

Examples:
```
Assert.notEqual(string("a"), "b");
// OK
foo.set(200)
Assert.notEqual(foo.get(), 200, "value should not be 200");
// error: value should not be 200
```

### Assert.greaterThan(value1, value2[, message])
* `value1`: \<uint | int\>
* `value2`: \<uint | int\>
* `message`: \<string\>

Tests if `value1` is greater than `value2`. `message` is returned in case of failure.

Examples:
```
Assert.greaterThan(uint(2), uint(1));
// OK
Assert.greaterThan(uint(-2), uint(1));
// OK
Assert.greaterThan(int(2), int(1));
// OK
Assert.greaterThan(int(-2), int(-1), "-2 is not greater than -1");
// error: -2 is not greater than -1
```

### Assert.lesserThan(value1, value2[, message])
* `value1`: \<uint | int\>
* `value2`: \<uint | int\>
* `message`: \<string\>

Tests if `value1` is lesser than `value2`. `message` is returned in case of failure.

Examples:
```
Assert.lesserThan(int(-2), int(-1));
// OK
Assert.lesserThan(int(2), int(1), "2 is not lesser than 1");
// error: 2 is not greater than 1
```
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Useful links:
remix_commands
remixd
unittesting
assert_library

.. toctree::
:maxdepth: 2
Expand Down
53 changes: 25 additions & 28 deletions docs/unittesting.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,46 @@ Unit Testing

Click the
![double check](images/a-user-testing-icon.png)
icon to get to the "Solidity Unit Testing" plugin. If you don't see this icon, go to the plugin manager (by click the ![plug](images/a-plug.png) icon) and load up the unit testing plugin.
icon to get to the "Solidity Unit Testing" plugin.

If you haven't used this plugin before and are not seeing `double check` icon, you have to activate it from Remix plugin manager.

Go to the plugin manager (by click the ![plug](images/a-plug.png) icon) and load up the unit testing plugin.

![](images/a-unit-testing-from-pm.png)

Now `double check` icon will appear on the left side icon bar. Clicking on icon will load the unit testing module in the side panel.

![](images/a-unit-testing-feature.png)

Generating Test File
Generate Test File
------------------
Click the button "Generate test file" to create a new solidity file in the current folder.
This create a new solidity file suffixed with `_test`.
This file contains the minimum you need for running unit testing.
Click the button `Generate test file` to create a new solidity file in the current folder suffixed with `_test`. This file contains the minimum you need for running unit testing.

Write Tests
-----------
Write tests to check the functionality of your contract. Remix injects a built-in `assert` library which can be used for testing. Visit the library documentation [here](./assert_library).

Apart from this, Remix allows usage of some special functions to make testing more structural. They are:

Running Tests
* `beforeEach()` - Runs before each test
* `beforeAll()` - Runs before all tests
* `afterEach()` - Runs after each test
* `afterAll()` - Runs after all tests

To get started, see [this](https://github.com/ethereum/remix/blob/master/remix-tests/tests/examples_4/SafeMath_test.sol) for sample implementation.

Run Tests
------------------

Click the button "Run tests" to executes all tests whose box has been checked below (by default all). The execution is run in a separate environment and the result is displayed below.

![](images/a-unit-testing-run-result.png)

Here is a list of functions and their supported types that you can use to write your testcases:

```eval_rst
+ -----------------------+--------------------------------------------------------+
| Available functions | Supported types |
+========================+========================================================+
| `Assert.ok()` | `bool` |
+------------------------+--------------------------------------------------------+
| `Assert.equal()` | `uint`, `int`, `bool`, `address`, `bytes32`, `string` |
+------------------------+--------------------------------------------------------+
| `Assert.notEqual()` | `uint`, `int`, `bool`, `address`, `bytes32`, `string` |
+------------------------+--------------------------------------------------------+
| `Assert.greaterThan()` | `uint`, `int` |
+------------------------+--------------------------------------------------------+
| `Assert.lesserThan()` | `uint`, `int` |
+------------------------+--------------------------------------------------------+
```

Click [here](https://github.com/ethereum/remix/blob/master/remix-tests/tests/examples_4/SafeMath_test.sol) for a test file example

Continuous integration
----------------------

ryestew marked this conversation as resolved.
Show resolved Hide resolved
remix-tests is also a CLI, it can be used in a continuous integration environement which support node.js.
Please find more information in the [remix-test repository](https://github.com/ethereum/remix/tree/master/remix-tests)
remix-tests is also a CLI, it can be used in a continuous integration environment which support node.js.
Please find more information in the [remix-tests repository](https://github.com/ethereum/remix/tree/master/remix-tests)

See also: example [Su Squares contract](https://github.com/su-squares/ethereum-contract/tree/e542f37d4f8f6c7b07d90a6554424268384a4186) and [Travis build](https://travis-ci.org/su-squares/ethereum-contract/builds/446186067) that uses remix-tests for continuous integration testing.