Swan Lake Beta3 is here.
import ballerina/test;

@test:Config { }
function testAssertEquals() {
    json a = {name:"John Doe", age:25, address:{city:"Colombo", 
    country:"Sri Lanka"}};
    json b = {name:"John Doe", age:25, address:{city:"Colombo", 
    country:"Sri Lanka"}};
    // Asserts two values for value equality using `assertEquals`.
    test:assertEquals(a, b, msg = "JSON values are not equal");
}

@test:Config { }
function testAssertNotEquals() {
    string s1 = "abc";
    string s2 = "def";
    // Asserts two values for value inequality using `assertNotEquals`.
    test:assertNotEquals(s1, s2, msg = "String values are equal");
}

@test:Config { }
function testAssertTrue() {
    boolean value = true;
    // Asserts if the provided value is `true`.
    test:assertTrue(value, msg = "AssertTrue failed");
}

@test:Config { }
function testAssertFalse() {
    boolean value = false;
    // Asserts if the provided value is `false`.
    test:assertFalse(value, msg = "AssertFalse failed");
}

@test:Config { }
function testAssertFail() {
    if (true) {
        return;
    }
    // Intentionally, throws an `AssertionError`.
    test:assertFail(msg = "AssertFailed");
}

class Person {
    public string name = "";
    public int age = 0;
    public Person? parent = ();
    private string email = "default@abc.com";
    string address = "No 20, Palm grove";
}

@test:Config { }
function testAssertExactEquals() {
    Person person1 = new;
    Person person2 = person1;
    // Compares the values for exact equality i.e. whether both refer to the same entity.
    test:assertExactEquals(person1, person2,
        msg = "Objects are not exactly equal");
}

@test:Config { }
function testAssertNotExactEquals() {
    Person person1 = new;
    Person person2 = new;
    // Compares the values for the negation of exact equality.
    test:assertNotExactEquals(person1, person2,
        msg = "Objects are exactly equal");
}

Assertions

Testerina has in-built assertions that enable you to assert an outcome against an expected outcome. This example illustrates how to use different assertions.

For more information, see Testing Ballerina Code and the Test Module.

import ballerina/test;
@test:Config { }
function testAssertEquals() {
    json a = {name:"John Doe", age:25, address:{city:"Colombo", 
    country:"Sri Lanka"}};
    json b = {name:"John Doe", age:25, address:{city:"Colombo", 
    country:"Sri Lanka"}};
    test:assertEquals(a, b, msg = "JSON values are not equal");
}

Asserts two values for value equality using assertEquals.

@test:Config { }
function testAssertNotEquals() {
    string s1 = "abc";
    string s2 = "def";
    test:assertNotEquals(s1, s2, msg = "String values are equal");
}

Asserts two values for value inequality using assertNotEquals.

@test:Config { }
function testAssertTrue() {
    boolean value = true;
    test:assertTrue(value, msg = "AssertTrue failed");
}

Asserts if the provided value is true.

@test:Config { }
function testAssertFalse() {
    boolean value = false;
    test:assertFalse(value, msg = "AssertFalse failed");
}

Asserts if the provided value is false.

@test:Config { }
function testAssertFail() {
    if (true) {
        return;
    }
    test:assertFail(msg = "AssertFailed");
}

Intentionally, throws an AssertionError.

class Person {
    public string name = "";
    public int age = 0;
    public Person? parent = ();
    private string email = "default@abc.com";
    string address = "No 20, Palm grove";
}
@test:Config { }
function testAssertExactEquals() {
    Person person1 = new;
    Person person2 = person1;
    test:assertExactEquals(person1, person2,
        msg = "Objects are not exactly equal");
}

Compares the values for exact equality i.e. whether both refer to the same entity.

@test:Config { }
function testAssertNotExactEquals() {
    Person person1 = new;
    Person person2 = new;
    test:assertNotExactEquals(person1, person2,
        msg = "Objects are exactly equal");
}

Compares the values for the negation of exact equality.

bal test test_module
Compiling source
        ballerinatest/test_module:0.1.0
Running tests
    ballerinatest/test_module:0.1.0
                [pass] testAssertEquals
                [pass] testAssertExactEquals
                [pass] testAssertFail
                [pass] testAssertFalse
                [pass] testAssertNotEquals
                [pass] testAssertNotExactEquals
                [pass] testAssertTrue
                7 passing
                0 failing
                0 skipped
import ballerina/test;

@test:Config { }
function testAssertEquals() {
    json a = {name:"John Doe", age:25, address:{city:"Colombo", 
    country:"Sri Lanka"}};
    json b = {name:"John Doe", age:25, address:{city:"Colombo", 
    country:"Sri Lanka"}};
    // Asserts two values for value equality using `assertEquals`.
    test:assertEquals(a, b, msg = "JSON values are not equal");
}

@test:Config { }
function testAssertNotEquals() {
    string s1 = "abc";
    string s2 = "def";
    // Asserts two values for value inequality using `assertNotEquals`.
    test:assertNotEquals(s1, s2, msg = "String values are equal");
}

@test:Config { }
function testAssertTrue() {
    boolean value = true;
    // Asserts if the provided value is `true`.
    test:assertTrue(value, msg = "AssertTrue failed");
}

@test:Config { }
function testAssertFalse() {
    boolean value = false;
    // Asserts if the provided value is `false`.
    test:assertFalse(value, msg = "AssertFalse failed");
}

@test:Config { }
function testAssertFail() {
    if (true) {
        return;
    }
    // Intentionally, throws an `AssertionError`.
    test:assertFail(msg = "AssertFailed");
}

class Person {
    public string name = "";
    public int age = 0;
    public Person? parent = ();
    private string email = "default@abc.com";
    string address = "No 20, Palm grove";
}

@test:Config { }
function testAssertExactEquals() {
    Person person1 = new;
    Person person2 = person1;
    // Compares the values for exact equality i.e. whether both refer to the same entity.
    test:assertExactEquals(person1, person2,
        msg = "Objects are not exactly equal");
}

@test:Config { }
function testAssertNotExactEquals() {
    Person person1 = new;
    Person person2 = new;
    // Compares the values for the negation of exact equality.
    test:assertNotExactEquals(person1, person2,
        msg = "Objects are exactly equal");
}

Assertions

Testerina has in-built assertions that enable you to assert an outcome against an expected outcome. This example illustrates how to use different assertions.

For more information, see Testing Ballerina Code and the Test Module.

import ballerina/test;
@test:Config { }
function testAssertEquals() {
    json a = {name:"John Doe", age:25, address:{city:"Colombo", 
    country:"Sri Lanka"}};
    json b = {name:"John Doe", age:25, address:{city:"Colombo", 
    country:"Sri Lanka"}};
    test:assertEquals(a, b, msg = "JSON values are not equal");
}

Asserts two values for value equality using assertEquals.

@test:Config { }
function testAssertNotEquals() {
    string s1 = "abc";
    string s2 = "def";
    test:assertNotEquals(s1, s2, msg = "String values are equal");
}

Asserts two values for value inequality using assertNotEquals.

@test:Config { }
function testAssertTrue() {
    boolean value = true;
    test:assertTrue(value, msg = "AssertTrue failed");
}

Asserts if the provided value is true.

@test:Config { }
function testAssertFalse() {
    boolean value = false;
    test:assertFalse(value, msg = "AssertFalse failed");
}

Asserts if the provided value is false.

@test:Config { }
function testAssertFail() {
    if (true) {
        return;
    }
    test:assertFail(msg = "AssertFailed");
}

Intentionally, throws an AssertionError.

class Person {
    public string name = "";
    public int age = 0;
    public Person? parent = ();
    private string email = "default@abc.com";
    string address = "No 20, Palm grove";
}
@test:Config { }
function testAssertExactEquals() {
    Person person1 = new;
    Person person2 = person1;
    test:assertExactEquals(person1, person2,
        msg = "Objects are not exactly equal");
}

Compares the values for exact equality i.e. whether both refer to the same entity.

@test:Config { }
function testAssertNotExactEquals() {
    Person person1 = new;
    Person person2 = new;
    test:assertNotExactEquals(person1, person2,
        msg = "Objects are exactly equal");
}

Compares the values for the negation of exact equality.

bal test test_module
Compiling source
        ballerinatest/test_module:0.1.0
Running tests
    ballerinatest/test_module:0.1.0
                [pass] testAssertEquals
                [pass] testAssertExactEquals
                [pass] testAssertFail
                [pass] testAssertFalse
                [pass] testAssertNotEquals
                [pass] testAssertNotExactEquals
                [pass] testAssertTrue
                7 passing
                0 failing
                0 skipped
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand