Popcorn testing services

Blackbox testing

Popcorn allows you to test your apps using nitunit blackbox testing.

With blackbox testing you compare the output of your program with a result file.

To get started with blackbox testing, create a nitunit test suite and imports the pop_tests module.

You then need to build the app that will be tested by nitunit as shown in the test_example_hello method. Calling run_test will automatically set the host and port used for testing.

Redefine the client_test method to write your scenario. Here we use curl to access some URI on the app.

module test_example_hello is test

import pop_tests
import example_hello

class TestExampleHello
    super TestPopcorn
    test

    fun example_hello is test do
        var app = new App
        app.use("/", new HelloHandler)
        run_test(app)
    end

    redef fun client_test do
        system "curl -s {host}:{port}"
        system "curl -s {host}:{port}/"
        system "curl -s {host}:{port}///////////"
        system "curl -s {host}:{port}/not_found"
        system "curl -s {host}:{port}/not_found/not_found"
    end
end

The blackbox testing needs a reference result file against wich the test output will be compared. Create your expected result file in test_example_hello.sav/test_example_hello.res.

Test your app by running nitunit:

nitunit ./example_hello.nit

See examples/hello_world for the complete example.

Introduced classes

class AppThread

popcorn :: AppThread

Thread running the App to test.
class ClientThread

popcorn :: ClientThread

Thread running the test client.
class TestPopcorn

popcorn :: TestPopcorn

TestSuite for Popcorn blackbox testing.

Redefined classes

redef class Sys

popcorn :: pop_tests $ Sys

The main class of the program.

All class definitions

class AppThread

popcorn $ AppThread

Thread running the App to test.
class ClientThread

popcorn $ ClientThread

Thread running the test client.
redef class Sys

popcorn :: pop_tests $ Sys

The main class of the program.
class TestPopcorn

popcorn $ TestPopcorn

TestSuite for Popcorn blackbox testing.
package_diagram popcorn::pop_tests pop_tests popcorn popcorn popcorn::pop_tests->popcorn github github popcorn->github config config popcorn->config csv csv popcorn->csv json json popcorn->json logger logger popcorn->logger realtime realtime popcorn->realtime mongodb mongodb popcorn->mongodb nitcorn nitcorn popcorn->nitcorn pthreads pthreads popcorn->pthreads template template popcorn->template ...github ... ...github->github ...config ... ...config->config ...csv ... ...csv->csv ...json ... ...json->json ...logger ... ...logger->logger ...realtime ... ...realtime->realtime ...mongodb ... ...mongodb->mongodb ...nitcorn ... ...nitcorn->nitcorn ...pthreads ... ...pthreads->pthreads ...template ... ...template->template

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module base64

base64 :: base64

Offers the base 64 encoding and decoding algorithms
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module caching

serialization :: caching

Services for caching serialization engines
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module console

console :: console

Defines some ANSI Terminal Control Escape Sequences.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module csv

csv :: csv

CSV document handling.
module engine_tools

serialization :: engine_tools

Advanced services for serialization engines
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module file_server

nitcorn :: file_server

Provides the FileServer action, which is a standard and minimal file server
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module http_errors

nitcorn :: http_errors

Offers ErrorTemplate to display error pages
module http_request

nitcorn :: http_request

Provides the HttpRequest class and services to create it
module http_request_buffer

nitcorn :: http_request_buffer

Http request parsing for buffered inputs.
module http_response

nitcorn :: http_response

Provides the HttpResponse class and http_status_codes
module inspect

serialization :: inspect

Refine Serializable::inspect to show more useful information
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module libevent

libevent :: libevent

Low-level wrapper around the libevent library to manage events on file descriptors
module list

core :: list

This module handle double linked lists
module logger

logger :: logger

A simple logger for Nit
module math

core :: math

Mathematical operations
module md5

md5 :: md5

Native MD5 digest implementation as Text::md5
module media_types

nitcorn :: media_types

Services to identify Internet media types (or MIME types, Content-types)
module meta

meta :: meta

Simple user-defined meta-level to manipulate types of instances as object.
module more_collections

more_collections :: more_collections

Highly specific, but useful, collections-related classes.
module native

core :: native

Native structures for text and bytes
module nitcorn

nitcorn :: nitcorn

The nitcorn Web server framework creates server-side Web apps in Nit
module numeric

core :: numeric

Advanced services for Numeric types
module pop_handlers

popcorn :: pop_handlers

Route handlers.
module pop_routes

popcorn :: pop_routes

Internal routes representation.
module pop_sessions

popcorn :: pop_sessions

Session handlers
module pop_tasks

popcorn :: pop_tasks

Popcorn threaded tasks
module poset

poset :: poset

Pre order sets and partial order set (ie hierarchies)
module protocol

core :: protocol

module pthreads

pthreads :: pthreads

Main POSIX threads support and intro the classes Thread, Mutex and Barrier
module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module reactor

nitcorn :: reactor

Core of the nitcorn project, provides HttpFactory and Action
module realtime

realtime :: realtime

Services to keep time of the wall clock time
module ropes

core :: ropes

Tree-based representation of a String.
module serialization

serialization :: serialization

General serialization services
module serialization_core

serialization :: serialization_core

Abstract services to serialize Nit objects to different formats
module server_config

nitcorn :: server_config

Classes and services to configure the server
module sessions

nitcorn :: sessions

Automated session management
module signal_handler

nitcorn :: signal_handler

Handle SIGINT and SIGTERM to close the server after all active events
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module template

template :: template

Basic template system
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module token

nitcorn :: token

Simple generate_token service, independent of the rest of the nitcorn framework
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O
module vararg_routes

nitcorn :: vararg_routes

Routes with parameters.

Parents

module popcorn

popcorn :: popcorn

Application server abstraction on top of nitcorn.

Descendants

module a_star-m

a_star-m

# Popcorn testing services
#
# ## Blackbox testing
#
# Popcorn allows you to test your apps using nitunit blackbox testing.
#
# With blackbox testing you compare the output of your program with a result file.
#
# To get started with blackbox testing, create a nitunit test suite and imports
# the `pop_tests` module.
#
# You then need to build the app that will be tested by nitunit as shown in the
# `test_example_hello` method.
# Calling `run_test` will automatically set the `host` and `port` used for testing.
#
# Redefine the `client_test` method to write your scenario.
# Here we use `curl` to access some URI on the app.
#
# ~~~nitish
# module test_example_hello is test
#
# import pop_tests
# import example_hello
#
# class TestExampleHello
#	super TestPopcorn
#	test
#
#	fun example_hello is test do
#		var app = new App
#		app.use("/", new HelloHandler)
#		run_test(app)
#	end
#
#	redef fun client_test do
#		system "curl -s {host}:{port}"
#		system "curl -s {host}:{port}/"
#		system "curl -s {host}:{port}///////////"
#		system "curl -s {host}:{port}/not_found"
#		system "curl -s {host}:{port}/not_found/not_found"
#	end
# end
# ~~~
#
# The blackbox testing needs a reference result file against wich the test output
# will be compared.
# Create your expected result file in `test_example_hello.sav/test_example_hello.res`.
#
# Test your app by running nitunit:
#
# ~~~bash
# nitunit ./example_hello.nit
# ~~~
#
# See `examples/hello_world` for the complete example.
module pop_tests

import popcorn
import pthreads

redef class Sys

	# Use localhost for testing
	var test_host = "localhost"

	# Return a new port for each instance
	fun test_port: Int do
		return testing_id % 20000 + 10000
	end

	# Nitdoc testing ID
	fun testing_id: Int do return "NIT_TESTING_ID".environ.to_i
end

# Thread running the App to test.
class AppThread
	super Thread

	# Host used by tested App.
	var host: String

	# Port used by tested App.
	var port: Int

	# App to test.
	var app: App

	redef fun main
	do
		# Hide testing concept to force nitcorn to actually run
		"NIT_TESTING".setenv("false")
		app.quiet = true
		app.listen(host, port)
		return null
	end
end

# Thread running the test client.
class ClientThread
	super Thread

	# Test suite to execute.
	var test_suite: TestPopcorn

	redef fun main do
		test_suite.client_test
		print ""
		return null
	end
end

# TestSuite for Popcorn blackbox testing.
class TestPopcorn

	# Host used to run App.
	var host: String = test_host

	# Port used to run App.
	var port: Int = test_port

	# Directory of the current test suite
	#
	# Useful when your tested app need to load some external files.
	var test_path: String = "NIT_TESTING_PATH".environ.dirname

	# Run the test suite on the App.
	fun run_test(app: App) do
		var server = new AppThread(host, port, app)
		server.start
		0.1.sleep

		var client = new ClientThread(self)
		client.start
		client.join
		0.1.sleep

		exit 0
	end

	# Redefine this method to implement your test scenario.
	fun client_test do end

	# Regex to catch and hide the port from the output to get consistent results
	var host_re: Regex = "localhost:\[0-9\]+".to_re

	# Execute a System function.
	fun system(cmd: String, title: nullable String)
	do
		title = title or else cmd
		title = title.replace(host_re, "localhost:*****")
		print "\n[Client] {title}"
		sys.system cmd
	end
end
lib/popcorn/pop_tests.nit:15,1--168,3