Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

testing-code-js

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

testing-code-js

Node.js module to create Test Classes and run it

  • 0.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

TestingCode

It is a javascript library to support unity tests. It was made like that way to improve flexibility to create and run tests. It, firstly, run at node.js that has a portable version. Link to portable node.js: http://imsky.co/files/nodejs-0.10.0.zip Once that perform the download, it is enough put the directory where the files was extracted in enviroment variable PATH of operational system.

How to use

Lets start with that basic example.

(function () {
  
	var StringType = typeof '', NumberType = typeof 1, UndefinedType = typeof undefined, ObjectType = typeof {},
	TestingCode = require('./TestingCode'), Math = {
		sum: function(op1, op2) {
			if(typeof op1!==NumberType)
				throw new Error('The first operator is not a number');
			if(typeof op2!==NumberType)
				throw new Error('The second operator is not a number');
			return op1 + op2;
		}
	};

	var math = {
		settings:{
			name:'MathTests'
		},
		tests: {
			when_sum_1_with_1_must_has_2_as_result:function(context){
				context.asserts.assertEquals([2, Math.sum(1,1)], 'The sum value of 1 + 1 was not as expected.');
			},
			when_sum_2_with_2_must_has_4_as_result:function(context){
				context.asserts.assertEquals([4, Math.sum(2,2)], 'The sum value of 2 + 2 was not as expected.');
			},
			when_sum_1_with_4_must_has_5_as_result:function(context){
				context.asserts.assertEquals([5, Math.sum(1, 4)], 'The sum value of 1 + 4 was not as expected.');
			}
		}
	};
	
	TestingCode.run([math]);
  
})();

Through of TestingCode class it is possible makes test classes and run it. In the case above, we have a class Math that contains a function "sum", which must be tested.

To create a test class

May can create a definition of test class like testClass that you can see below:

var testClass = {
  settings:{
		name:'TestingClass', //Name of test class to show in a console output 
		setup:function(context){}, //Function that will be executed once by test method at first
		finalize:function(context){}, //Function that will be executed once by test method at end
  },
  tests:{
    //Test methods must be putted here
    test1:function(context){ ... },
    test2:function(context){ ... }
  }
}; 

So, lets improve the old example. Below, we are going to execute 2 test classes and the last one has an implementation of setup function to initialize the argument 'operator1'. Also, it will put 2 sentences in console, one when perform setup and the other when perform finalize.

(function () {
  var StringType = typeof '', NumberType = typeof 1, UndefinedType = typeof undefined, ObjectType = typeof {},
		TestingCode = require('./TestingCode'), Math = {
			sum: function(op1, op2) {
				if(typeof op1!==NumberType)
					throw new Error('The first operator is not a number');
				if(typeof op2!==NumberType)
					throw new Error('The second operator is not a number');
				return op1 + op2;
			}
		};
	
	var math = {
		settings:{
			name:'MathTests'
		},
		tests: {
			when_sum_1_with_1_must_has_2_as_result:function(context){
				context.asserts.assertEquals([2, Math.sum(1,1)], 'The sum value of 1 + 1 was not as expected.');
			},
			when_sum_2_with_2_must_has_4_as_result:function(context){
				context.asserts.assertEquals([4, Math.sum(2,2)], 'The sum value of 2 + 2 was not as expected.');
			},
			when_sum_1_with_4_must_has_5_as_result:function(context){
				context.asserts.assertEquals([5, Math.sum(1, 4)], 'The sum value of 1 + 4 was not as expected.');
			}
		}
	}, math2 = {
		settings:{
			name:'MathTests2',
			//Function that will be executed once by test method at first
			setup:function(context){ 
				console.log('Starting ' + math2.settings.name);
				// Use context.setArgs to apply some arguments to each test
				context.setArgs({
					//Some argument that will be used by test methods
					//May be accessed by context, like context.args.variable_name_here
					operator1:2
				}); 
			}, 
			//Function that will be executed once by test method at end
			finalize:function(context){ console.log('Finalizing ' + math2.settings.name); } //Function that will be executed once by test method at end
		},
		tests: {
			when_sum_2_with_1_must_has_3_as_result:function(context){
				context.asserts.assertEquals([3, Math.sum(parseInt(context.args.operator1),1)], 'The sum value of 2 + 1 was not as expected.');
			},
			when_sum_2_with_2_must_has_4_as_result:function(context){
				context.asserts.assertEquals([4, Math.sum(parseInt(context.args.operator1),2)], 'The sum value of 2 + 2 was not as expected.');
			},
			when_sum_2_with_4_must_has_6_as_result:function(context){
				context.asserts.assertEquals([6, Math.sum(parseInt(context.args.operator1), 4)], 'The sum value of 2 + 4 was not as expected.');
			}
		}
	};
	
	globalTestContext = TestingCode.run([math, math2]);
	//You may use globalTestContext to create a report with some values
	//globalTestContext.getTotalTests(), globalTestContext.getExecutionTimes(), 
	//globalTestContext.getTotalSuccess(), globalTestContext.getTotalFailures(), 
	//globalTestContext.getTotalUndefined(), globalTestContext.tests[] { 
	//	{ 
	//		testName: "methodName",
	//		executed: boolean,
	//		successed: boolean,
	//		failed: boolean,
	//		successed: boolean,
	//		assertMessages: string[]
	//	},
	//	{ 
	//		testName: "methodName",
	//		executed: boolean,
	//		successed: boolean,
	//		failed: boolean,
	//		successed: boolean,
	//		assertMessages: string[]
	//	} 
	//}
})();

So, that is it! Easy like that.

Keywords

FAQs

Package last updated on 28 Jul 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc