An example to begin with


In this example, we will show you a simple SKF script that uses macros to do tests against an H2 database in embedded mode.



Create a project

First of all you need to open your favorite IDE and create a new maven project with squash-ta-archetype.

If you don’t know how to generate a maven archetype, you can follow our guide.

Delete all the samples in the folders of the generated project. Just keep the structure :

../_images/clean-skf-project1.png


Configure database

For the database, you need to add the following dependency to your POM file :

<dependencies>
     <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.3.176</version>
    </dependency>
</dependencies>

Your POM file should look like this :

../_images/pom-file.png

In the targets folder (be careful about the name, targets != target), you need to create a .properties file.

The .properties file should have the following properties :

  • #!db : The shebang to indicate that this file contains informations about a database.
  • squashtest.ta.database.driver : The driver to be used.
  • squashtest.ta.database.url : The path to the database.
  • squashtest.ta.database.username (optional, not used in our example) : The username to use to connect to the database.
  • squashtest.ta.database.password (optional, not used in our example) : The password to use to connect to the database.

In our example, it will be as follow :

#!db
squashtest.ta.database.driver=org.h2.Driver
squashtest.ta.database.url=jdbc:h2:file:target/database/myH2

.properties file to connect to a database :

../_images/properties-file1.png


Create SKF script

In tests folder create a .ta file.

In this file, write down :

SETUP :


TEST :


TEARDOWN :
../_images/skf-script-new.png

We will use those 3 phases in our example.



Execute an SQL script to create a table in database

First of all, during the SETUP phase, we want te create a new table in our H2 database.

To do so, we need to create a .sql script file in resources folder. It’s good practice to create different subfolders for each type of resources (sql, selenium, soapui, etc).

Here is the script :

DROP TABLE IF EXISTS PETS_STORE;

CREATE TABLE PETS_STORE (
        ID INT NOT NULL,
        ANIMAL VARCHAR(45) NULL,
        COLOR VARCHAR(45) NULL,
        QUANTITY INT NULL,
        PRIMARY KEY (id)
);
../_images/sql-script.png

In the SKF script, add the following macro to your SETUP phase :

# EXECUTE_SQL_SCRIPT {file} ON {database} AS {result}

{file} : The SQL script, we have just created. Give the path of the file in the resources folder.

{database} : The database we want to operate the script on. Give the name of the .properties file you have created in the targets folder (without the .properties extension).

{result} : A free identifier for the result. As the ‘execute’ command with an sql script return an empty resource, this result resource will also be empty.

../_images/skf-script-sql-script-macro.png


Populate the database table with a DbUnit dataset

The populate the table, we will use a DbUnit dataset.

Create an .xml file in resources folder. You should also create a dbunit subfolder.

In this file, write down the following :

<?xml version="1.0" encoding="UTF-8"?>

<dataset>
    <PETS_STORE ID="1" ANIMAL="cat" COLOR="black" QUANTITY="4"/>
    <PETS_STORE ID="2" ANIMAL="cat" COLOR="white" QUANTITY="2"/>
    <PETS_STORE ID="3" ANIMAL="cat" COLOR="grey" QUANTITY="5"/>
    <PETS_STORE ID="4" ANIMAL="cat" COLOR="red hair" QUANTITY="2"/>
    <PETS_STORE ID="5" ANIMAL="cat" COLOR="invisible" QUANTITY="0"/>
</dataset>
../_images/dataset.png

In the SKF script, add the following macro to your SETUP phase :

# INSERT_DBUNIT {dataset} INTO {database}

{dataset} : The .xml, we have just created. Give the path of the file in the resources folder.

{database} : The database we want to operate the script on. Give the name of the .properties file you have created in the targets folder (without the .properties extension).

../_images/skf-script-insert-dbunit.png


Test that our table contains expected data with a DbUnit dataset

First we will do an incorrect dataset so that the assertion executed by the script fails.

Create a new .xml file in the resources/dbunit folder.

Write down the following dataset :

<?xml version="1.0" encoding="UTF-8"?>

<dataset>
    <PETS_STORE ID="1" ANIMAL="cat" COLOR="black" QUANTITY="4"/>
    <PETS_STORE ID="2" ANIMAL="cat" COLOR="green" QUANTITY="2"/>
</dataset>
../_images/partial-dataset-ko.png

In the SKF script, add the following macro to your TEST phase :

# ASSERT_DBUNIT TARGET {database} CONTAINS {dataset}
../_images/skf-script-assert-contains-macro-ko.png

Now we are going to execute th script. Use the following maven command to build your project :

mvn squash-ta:run
../_images/build-failure-assert-contains.png

After the execution, an HTML report is generated. It can give further details about the reason of the failure.

You can access this report in target/squashTA/html-reports folder :

../_images/html-report-location.png

Open this report with the web browser of your choice :

../_images/html-report-build-failure-assert-contains.png

You can the diffrences between the dataset and the database by opening EXECUTION_REPORT-diff in the attachments :

../_images/html-report-build-failure-assert-contains-attachment-file.png

Now we are going to create a new .xml file with a correct dataset :

<?xml version="1.0" encoding="UTF-8"?>

<dataset>
    <PETS_STORE ID="1" ANIMAL="cat" COLOR="black" QUANTITY="4"/>
    <PETS_STORE ID="2" ANIMAL="cat" COLOR="white" QUANTITY="2"/>
</dataset>
../_images/partial-dataset.png

Don’t forget to change the dataset used in the SKF script :

../_images/skf-script-assert-contains-macro.png

If you execute the script again, you should have a build SUCCESS.



Test that our table contains all the expected data

As in the previous example, we will start with an incorrect dataset.

Create a new .xml file in the resources/dbunit folder and write down the following dataset :

<?xml version="1.0" encoding="UTF-8"?>

<dataset>
    <PETS_STORE ID="1" ANIMAL="cat" COLOR="black" QUANTITY="4"/>
    <PETS_STORE ID="2" ANIMAL="cat" COLOR="white" QUANTITY="2"/>
    <PETS_STORE ID="3" ANIMAL="cat" COLOR="grey" QUANTITY="5"/>
    <PETS_STORE ID="4" ANIMAL="cat" COLOR="red hair" QUANTITY="2"/>
</dataset>

The invisible cat is missing.

../_images/dataset-ko.png

In the SKF script, add the following macro to your TEST phase :

# ASSERT_DBUNIT TARGET {database} EQUALS {dataset}
../_images/skf-script-assert-equals-macro-ko.png

Execute the script. You should have a build failure with the following error :

../_images/build-failure-assert-equals.png

You can open the HTML report to have more details :

../_images/html-report-build-failure-assert-equals.png

In SKF script, change the dataset in the last macro and use the first one we created to populate the table :

../_images/skf-script-assert-equals-macro.png

If you execute the script again, you should have a build SUCCESS.



Clean the database

The last thing we want to do is to clean the database after the execution of the test.

In SKF script, add the following macro in TEARDOWN phase :

# DELETE_DBUNIT {dataset} FROM {database}
../_images/skf-script-teardown.png