Giter Site home page Giter Site logo

testor's Introduction

Testor

An easy-to-use testing framework for web app of Node.js, support HTTP and HTTPS, GET and POST.

Install

npm install testor -g && npm install testor --save-dev

Usage

1. Create folder "test" under your project root path

2. Create file "./test/cases.js". (See Define test cases to learn more)

3. Run test under your project root path

testor

The results will be like below:

  01 test web app
    ✓ /about
    ✓ /say/hi
    ✓ /say/hi?name=owen&age=100
    ✓ /helloWorld

  4 passing (1s)

Define test cases

Use an array to define test cases in file "./test/cases.js".

1. Minimal definition

A target url with a result object

module.exports = [
    
    // The target url which will be tested. 
    // It will be completed like "http://localhost:3000/about" if it is not started with "http:..."
    '/about', 

    // The expected result which should be returned from the server.
    // The result can be any structure. The following is Noapi style.
    {
        success: true,
        data: {
            "version": "1.0.0"
        }
    },
];

2. Basic definition

1) With "method", "params", "result" and "verify"

module.exports = [
    
    '/say/hi',
    {
        // The default is POST
        method: 'GET', 

        // The params with test data will be send to the target url 
        params: { 
            name: 'owen',
            age: 100
        },

        // The expected result which should be returned from the server.
        result: {
            success: true,
            data: {
                msg: "Hi, I am owen, 100 years old."
            }
        },
        
        // If a verify() function is specified, it will be used
        // instead of the property "result" to verify the result.
        verify(result) {
            
            // The result is returned from the server at runtime.
            return result.data.msg === 'Hi, I am owen, 100 years old.';
        }
    },    
];

2) Just "result"

module.exports = [
    '/say/hi?name=owen&age=100',
    {
        result: {
            success: true,
            data: {
                msg: "Hi, I am owen, 100 years old."
            }
        }
    },    
];

3) Just "params" and "verify"

module.exports = [
    '/say/hi',
    {
        params: {
            name: 'owen',
            age: 100
        },

        verify(result) {
            // The result is returned from the server at runtime.
            return result.data.msg === 'Hi, I am owen, 100 years old.';
        }
    },
];

4) Just a function

module.exports = [
    '/helloWorld',
    
    // If it is a function, it is the verify function
    (result) => {
    
        // The result is returned from the server at runtime.
        return result.data.msg.indexOf('Hello') >= 0;
    },
];

3. Title of test case

You can specify a title string before url like below, Testor will prints it instead of the url.

module.exports = [
    
    'Passing test data via params, and using GET method',
    '/say/hi',
    {
        method: 'GET', // default is POST

        params: {
            name: 'owen',
            age: 100
        },

        result: {
            success: true,
            data: {
                msg: "Hi, I am owen, 100 years old."
            }
        }
    },

    'Passing test data via url',
    '/say/hi?name=owen&age=100',
    {
        result: {
            success: true,
            data: {
                msg: "Hi, I am owen, 100 years old."
            }
        }
    },

];

Result

  02 title of test cases
    ✓ Passing test data via params, and using GET method
    ✓ Passing test data via url

  2 passing (1s)

See demo file to learn more.

4. Before and after

Sometimes you wanna do something before test and after tested, use "before" and "after" like below. The "before" and "after" should be a url array or a url string.

module.exports = [
    
    // Step 2
    '/user/list',
    {
        // Step 1
        before: [
            '/user/register?username=owen&password=123',
            '/user/login?username=owen&password=123',
        ],

        // Step 3
        after: '/user/kill?username=owen',

        // Step 4: using the result returned from step 2
        verify(result) {
            const rst = result.data.find(item => item.username === 'owen');
            return !!rst;
        }
    },
    
];

More further, you can use "resultUrl" instead of the target url to get the result like below step 3.

module.exports = [
    
    // Step 2
    '/user/logout?username=owen',
    {
        // Step 1
        before: [
            '/user/register?username=owen&password=123',
            '/user/login?username=owen&password=123',
        ],

        // Step 3: using this url to get the result
        resultUrl: '/user/get?username=owen',

        // Step 4
        after: '/user/kill?username=owen',

        // Step 5: using the result returned from step 3 instead of step 2
        verify(result) {
            return result.data.isOnline === 0;
        }
    },    
        
];

See demo file to learn more.

5. Before and after with title

You can use the title of test case instead of url in "before" and "after".

module.exports = [

    'register',
    '/user/register?username=owen&password=123',
    {
        // Using the title instead of url
        before: 'kill',

        verify(result) {
            return result.data > 0;
        }
    },
    
    'kill',
    '/user/kill?username=owen',
    {
        verify(result) {
            return result.data === 1;
        }
    },
    
];

See demo file to learn more.

6. Before and after with scripts

You can write some scripts under directory "test", and use them in "before" and "after".

module.exports = [
    
    // Step 2
    '/user/logout?username=owen',
    {
        // Step 1
        before: [
            './scripts/user/register?username=owen&password=123',
            './scripts/user/login?username=owen&password=123',
        ],

        // Step 3: use this url to get the result
        resultUrl: './scripts/user/get?username=owen',

        // Step 4
        after: './scripts/user/kill?username=owen',

        // Step 5: use the result returned from step 3 instead of step 2
        verify(result) {
            return result.data.isOnline === 0;
        }
    },
    
];

See demo file to learn more.

Examples

CLi Options

1. Testor options

Specify the web server root path

By default, Testor will uses the current path as the web server root path.

testor

It is equivalents to testor ..

You can specify another path like this

testor /path/to/web/server/root

Or

testor ./web/server/root

--config

Apply the web server config.

testor --config

It is equivalents to testor --config=./config.js. You can specify another config file.

testor --config=./myconfig.js

--logs

Output server logs.

testor --logs

--wait

Time (milliseconds) to wait for the server to be ready. The default value is 1000.

testor --wait=2000

2. Mocha options

Testor uses mocha to run test cases. You can use some mocha CLi options in Testor.

-b, --bail

Force to bail after the first test failure.

testor --bail

-t, --timeout <ms>

The timeout of test cases. The default is 2 seconds.

testor -t 3000

It is equivalents to:

testor --timeout 3s

Use --no-timeouts or --timeout 0 to disable timeout:

testor --no-timeouts

--inspect-brk <port>

Debug tests running in Node using Chrome DevTools inspector.

testor --inspect-brk 9229

Then input chrome://inspect in your Chrome browser address bar, click the link "inspect" under Remote Target. Recommend NiM (Node.js --inspector Manager) which is more easy-to-use.

Test

git clone https://github.com/hiowenluke/testor
cd testor
npm install
npm test

License

MIT

Copyright (c) 2019, Owen Luke

testor's People

Contributors

hiowenluke avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

chailijia

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.