DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. SQLite-Driven Testing Using JavaScript

SQLite-Driven Testing Using JavaScript

This quick tutorial will help you learn how to structure tests using DDT, or Data Driven Testing, for very large data sets that may grow even more.

Reginald Stadlbauer user avatar by
Reginald Stadlbauer
·
May. 21, 18 · Tutorial
Like (5)
Save
Tweet
Share
6.58K Views

Join the DZone community and get the full member experience.

Join For Free

Data Driven Testing (DDT) is a method to structure tests. It is recommended for a huge set of data which may grow in the future. The code has to be written only once and new data can be fed into the test without touching the code at all, which is the biggest advantage of this strategy. Data can be loaded from a file or a database. I would like to show how this works in a small example with an address book. One example of it is available in every Squish edition. As the title already told you, we are going to fetch data from a SQLite database.

SQLite Database

To create a new database I downloaded the “DB Browser for SQLite” software from their homepage to do so. It’s straightforward to create the database and insert data via SQL. SQLite databases are file-based, thus they do not have a server (Host IP and Port) to connect to.

The following screen shows the DB Browser software and some data fetched via an SQL query. I used this website to generate random user data.

I used the address book example from a Squish for Windows package and JavaScript as the script language. The use of JavaScript grant access to the SQL Object which allows fetching of data without additional imports.

In the test script, we have to establish a “connection” to the database file. For more, we will use the SQL object proposed by Squish (see the documentation). With the “connection” ready, we can send our query and handle the return value. The if statement in the code is needed to make sure the database file isn’t empty.

function fetchDataFromSQLiteDB() {
    //used http://sqlitebrowser.org/ for creating database
    var conn = SQL.connect( { Driver: "SQLite",
    Database: "C:\\Users\\franke\\Desktop\\squish.db"} );

    var result = conn.query("SELECT id, forename, surname, email, phone FROM addressbook;");
    if(result.isValid == false) {
        test.log("Result is not valid, maybe no entries in database?")
    } else {
        while (result.isValid) {
            // do something with the result
            var id = result.value(0)
            var forename = result.value(1)
            var surname = result.value(2)
            var email = result.value(3)
            var phone = result.value(4)
            //test.log(id + forename + surname + email + phone)
            addEntry(forename, surname, email, phone)
            result.toNext();
        }
    test.log("added " + id + " entries in the addressbook application")
 }

To use this code properly, you have to start the application and create a new address book as a pre-condition. The “addEntry” function takes the fetched data as arguments and adds them into the address book.

Address Book After Running the Script

The following screenshot shows the address book example application after running the test script.

Beware that the test execution time increases by every additional entry in the database/data file.

JavaScript Address book Database Data (computing)

Published at DZone with permission of Reginald Stadlbauer. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Utilize OpenAI API to Extract Information From PDF Files
  • Agile Transformation With ChatGPT or McBoston?
  • Why It Is Important To Have an Ownership as a DevOps Engineer
  • What Is Policy-as-Code? An Introduction to Open Policy Agent

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: