Skip to content

Commit

Permalink
Update testerina.md [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
rasika committed Apr 19, 2019
1 parent 72c580e commit a3fadf8
Showing 1 changed file with 26 additions and 48 deletions.
74 changes: 26 additions & 48 deletions misc/testerina/README.md
Expand Up @@ -47,65 +47,47 @@ Testerina allows you to create mocks or doubles at different layers for testing
With Function mocks we can replace a function interface from the same module or from a different module with a mock function. Function mock will look like something below,

````ballerina
@Description {value:"This is a mock function"}
# This is a mock function
#
# + evnt - evnt Parameter Description
# + return - Return Value Description
@test:Mock {
moduleName:"src.persistence",
functionName:"addNewEvent"
}
function mockAddNewEvent (mod:Event evnt) returns json {
err = {message:"Error"};
error err = error("Error");
json jsonResponse = { "Success":"Created", "id":"2" };
return jsonResponse;
}
````

#### Service Mocks
### Helper Functions

Service mocks allow you to create your own service to mock the actual bach ends.
Helper functions allow you to control ballerina tests, services etc. Currently Testerina has the following helper functions.

Service mock will look something like below,
#### Data providers
Testerina natively support data driven testing. You can execute the same test function repetitively on distinct data sets by using data-providers.

````ballerina
import ballerina/net.http;
e.g:

endpoint http:Listener paymentGWEP {
port:9094
};
You can add a data provider based test functions as shown below,
```ballerina
import ballerina/test;
import ballerina/log;
@http:ServiceConfig {
endpoints:[paymentGWEP], basePath:"/boc"
@test:Config {
dataProvider: "testCalculateDataProvider"
}
service<http:Service> PaymentService bind paymentGWEP {
@http:ResourceConfig {
methods:["POST"],
path:"/payment"
}
creditOperations (endpoint conn, http:Request req, string eventID) {
// Expects an API Token
http:Response res = {};
json jsonRes = {"Payment":"Sucess!"};
res.statusCode = 200;
res.setJsonPayload(jsonRes);
_ = conn -> respond(res);
}
function testCalculate(int n1, int n2, int expected) {
int actual = calculate(n1, n2);
test:assertEquals(actual, expected, msg = string `Calculation is wrong for n1:${n1} and n2:${n2}!`);
}
````

The above mock service can be started as shown below,
````
test:startServices(<module_name>);
````

### Helper Functions

Helper functions allow you to control ballerina tests, services etc. Currently Testerina has the following helper functions.

#### Data providers
Testerina natively support data driven testing. You can execute the same test function repetitively on distinct data sets by using data-providers.
function testCalculateDataProvider() returns ((int, int, int)[]) {
return [(5, 5, 10), (10, 10, 20), (500, 500, 1000), (1000, -2000, -100)];
}
```

#### Test Groups
You can group your test functions and control the execution of tests by specifying the groups of tests you wish to execute.
Expand Down Expand Up @@ -135,8 +117,8 @@ e.g : ballerina test --list-groups

## Writing ballerina tests

- Test can reside in any valid ballerina module. As a Best practice we put them into a subdirectory called tests within a module
- Test file names and functions can have any name.
- Test should reside in a subdirectory called tests within a valid ballerina module.
- Test file names and functions can have any name. As a Best practice we name test file as \<source_name>_tests.
- We can execute all the tests or tests belonging to a specific module..

- If at least one assert fails, whole test function will be marked as failed.
Expand All @@ -147,8 +129,4 @@ e.g : ballerina test --list-groups

Tests can be execute as shown below

```./ballerina test <module_name> [FLAGS]``` Execute tests within a given module

or

```./ballerina test [FLAGS]``` Executes all the tests within modules
```./ballerina test <module_name> [FLAGS]``` Execute tests within a given module

0 comments on commit a3fadf8

Please sign in to comment.