appenginetesting

package module
v0.0.0-...-c1e2943 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2015 License: Apache-2.0 Imports: 25 Imported by: 0

README

appenginetesting

Build Status

This package provides an automated way to test Go based appengine applications. It differs from the appengine/aetest package as with appenginetesting the real application can be run alongside a stub module.*

E.g., Your application has the default module and two other modules (default, A, and B). You can run your application under appenginetesting and perform tests against it. Using the []ModuleConfig under Options tells what modules to start (default, A, and B) and appenginetesting additionally starts up a "mock" Context that can be used to generate and maniuplate data for testing.

Features

  • Login/Logout
  • Modules (formerly Backends)
  • Task Queues
  • Using *testing.T to Log (only spew logs on test failure)
  • Logging the SDK output to console (often helpful in debugging) (LogChild)
  • Data Generation
  • Leverages automatic creation/updating of index.yaml based on unit tests

History

It's a combined fork of gae-go-testing and aetest with a number

Installation

Before using this library, you have to install appengine SDK.

This library can be installed as following :

$ go get -u github.com/mzimmerman/appenginetesting

Usage

The documentation has some basic examples. Also see the example application.

func TestMyApp(t *testing.T) {
        c, err := appenginetesting.NewContext(&appenginetesting.Options{
                Debug:   appenginetesting.LogDebug,
                Testing: t,
        })
        if err != nil {
                t.Fatalf("Could not get a context - %v", err)
        }
        defer c.Close()
        // do things
        c.Debugf("Log stuff")
}

goapp test

For the details of various Options that can be used in NewContext, see http://godoc.org/github.com/mzimmerman/appenginetesting#Options

Documentation

Overview

Package appenginetesting provides an appengine.Context for testing.

Index

Examples

Constants

View Source
const AppServerFileName = "dev_appserver.py"

Dev app server script filename

Variables

This section is empty.

Functions

This section is empty.

Types

type ComponentURL

type ComponentURL struct {
	Name  string
	Regex *regexp.Regexp
	URL   string
}

type Context

type Context struct {
	// contains filtered or unexported fields
}

Context implements appengine.Context by running a dev_appserver.py process as a child and proxying all Context calls to the child. Use NewContext to create one.

func NewContext

func NewContext(opts *Options) (*Context, error)

NewContext returns a new AppEngine context with an empty datastore, etc. A nil Options is valid and means to use the default values.

Example (Datastore)
// Create mocked context.
c, err := NewContext(nil)
if err != nil {
	fmt.Println("initilizing context:", err)
	return
}

// Close the context when we are done.
defer c.Close()

// Get an element from the datastore.
k := datastore.NewKey(c, "Entity", "stringID", 0, nil)
e := ""
if err := datastore.Get(c, k, &e); err != nil {
	fmt.Println("datastore get:", err)
	return
}

// Put an element in the datastore.
if _, err := datastore.Put(c, k, &e); err != nil {
	fmt.Println("datastore put:", err)
	return
}
Output:

Example (User)
// Create mocked context.
c, err := NewContext(nil)
if err != nil {
	fmt.Println("initilizing context:", err)
	return
}

// Close the context when we are done.
defer c.Close()

// Log a user in.
c.Login(&user.User{Email: "test@example.com", Admin: true})

// Get the user.
u := user.Current(c)
if u == nil {
	fmt.Println("we didn't get a user!")
	return
}

fmt.Println("Email:", u.Email)
fmt.Println("Admin:", u.Admin)
Output:

Email: test@example.com
Admin: true

func (*Context) AppID

func (c *Context) AppID() string

func (*Context) Call

func (c *Context) Call(service, method string, in, out appengine_internal.ProtoMessage, opts *appengine_internal.CallOptions) error

func (*Context) Close

func (c *Context) Close()

Close kills the child dev_appserver.py process, releasing its resources.

Close is not part of the appengine.Context interface.

func (*Context) Criticalf

func (c *Context) Criticalf(format string, args ...interface{})

func (*Context) CurrentNamespace

func (c *Context) CurrentNamespace(namespace string)

func (*Context) CurrentUser

func (c *Context) CurrentUser() string

func (*Context) Debugf

func (c *Context) Debugf(format string, args ...interface{})

func (*Context) Errorf

func (c *Context) Errorf(format string, args ...interface{})

func (*Context) FullyQualifiedAppID

func (c *Context) FullyQualifiedAppID() string

func (*Context) GetCurrentNamespace

func (c *Context) GetCurrentNamespace() string

func (*Context) Infof

func (c *Context) Infof(format string, args ...interface{})

func (*Context) Login

func (c *Context) Login(u *user.User)

func (*Context) Logout

func (c *Context) Logout()

func (*Context) Request

func (c *Context) Request() interface{}

func (*Context) Warningf

func (c *Context) Warningf(format string, args ...interface{})

type ContextRecorder

type ContextRecorder struct {
	// contains filtered or unexported fields
}

func NewContextRecorder

func NewContextRecorder(opts *Options) *ContextRecorder

func (*ContextRecorder) Context

func (r *ContextRecorder) Context() *Context

func (*ContextRecorder) Creator

func (r *ContextRecorder) Creator() func(r *http.Request) appengine.Context

type LogLevel

type LogLevel int8
const (
	LogChild LogLevel = iota // LogChild logs all log levels plus what comes from the devappserver process
	LogDebug
	LogInfo
	LogWarning
	LogError
	LogCritical
)

func (LogLevel) String

func (ll LogLevel) String() string

type ModuleConfig

type ModuleConfig struct {
	Name string // name of the module in the yaml file
	Path string // can be relative to the current working directory and should include the yaml file
}

type Options

type Options struct {
	// AppId to pretend to be. By default, "testapp"
	AppId      string // Required if using any Modules
	TaskQueues []string
	Debug      LogLevel
	Testing    *testing.T
	Modules    []ModuleConfig
}

Options control optional behavior for NewContext.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL