testing

package
v0.0.0-20260608 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FakeHTTP

func FakeHTTP(handler http.Handler) *httptest.Server

FakeHTTP creates a fake HTTP server for testing.

func UnfreezeTime

func UnfreezeTime()

UnfreezeTime stops global time travel.

func WithoutExceptionHandling

func WithoutExceptionHandling(fn func())

WithoutExceptionHandling runs the given function with exception handling disabled.

Types

type ArtisanTestCase

type ArtisanTestCase struct {
	*testing.T
	// contains filtered or unexported fields
}

ArtisanTestCase provides testing utilities for artisan commands.

func NewArtisanTestCase

func NewArtisanTestCase(t *testing.T, args ...string) *ArtisanTestCase

NewArtisanTestCase creates a new artisan test case.

func (*ArtisanTestCase) Args

func (atc *ArtisanTestCase) Args(args ...string) *ArtisanTestCase

Args sets the command arguments.

func (*ArtisanTestCase) AssertExitCode

func (atc *ArtisanTestCase) AssertExitCode(code int) *ArtisanTestCase

AssertExitCode asserts the command exited with the given code.

func (*ArtisanTestCase) AssertOutputContains

func (atc *ArtisanTestCase) AssertOutputContains(str string) *ArtisanTestCase

AssertOutputContains asserts the output contains the given string.

func (*ArtisanTestCase) AssertOutputIs

func (atc *ArtisanTestCase) AssertOutputIs(expected string) *ArtisanTestCase

AssertOutputIs asserts the output is exactly the given string.

func (*ArtisanTestCase) AssertOutputNotContains

func (atc *ArtisanTestCase) AssertOutputNotContains(str string) *ArtisanTestCase

AssertOutputNotContains asserts the output does not contain the given string.

func (*ArtisanTestCase) AssertSuccessful

func (atc *ArtisanTestCase) AssertSuccessful() *ArtisanTestCase

AssertSuccessful asserts the command was successful (exit code 0).

func (*ArtisanTestCase) Output

func (atc *ArtisanTestCase) Output() string

Output returns the command output.

type ConcurrentTestCase

type ConcurrentTestCase struct {
	*testing.T
	// contains filtered or unexported fields
}

ConcurrentTestCase manages concurrent test execution with shared state.

func NewConcurrentTestCase

func NewConcurrentTestCase(t *testing.T, workers int, timeout time.Duration) *ConcurrentTestCase

NewConcurrentTestCase creates a new concurrent test case.

func (*ConcurrentTestCase) AssertNoErrors

func (ctc *ConcurrentTestCase) AssertNoErrors()

AssertNoErrors asserts that no errors occurred in the error channel.

func (*ConcurrentTestCase) Run

func (ctc *ConcurrentTestCase) Run(workers int, fn func(workerID int) error)

Run runs a function concurrently with the specified number of workers.

func (*ConcurrentTestCase) RunWithTimeout

func (ctc *ConcurrentTestCase) RunWithTimeout(timeout time.Duration, fn func() error)

RunWithTimeout runs a function with a timeout.

type DBTestContext

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

DBTestContext manages database state for tests.

func NewDBTestContext

func NewDBTestContext(db *sql.DB) *DBTestContext

NewDBTestContext initializes a new database testing context.

func (*DBTestContext) GetTransaction

func (c *DBTestContext) GetTransaction() *sql.Tx

GetTransaction returns the current transaction (if RefreshDatabase was used).

func (*DBTestContext) RefreshDatabase

func (c *DBTestContext) RefreshDatabase(t *testing.T, testFunc func())

RefreshDatabase wraps a test in a transaction and automatically rolls it back after the test. This is the recommended way to keep tests isolated and fast.

func (*DBTestContext) SetupSuite

func (c *DBTestContext) SetupSuite()

SetupSuite should be called once before all tests run. It applies all migrations to the database.

func (*DBTestContext) UseInMemorySQLite

func (c *DBTestContext) UseInMemorySQLite(t *testing.T, testFunc func(db *sql.DB))

UseInMemorySQLite sets up a completely fresh SQLite database in memory for the test.

func (*DBTestContext) WithTransaction

func (c *DBTestContext) WithTransaction(t *testing.T, fn func(tx *sql.Tx))

WithTransaction is a helper that lets you run a block inside a transaction. Useful when you want to share the same transaction across multiple operations in a test.

type FakeClock

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

FakeClock provides time control for testing.

func NewFakeClock

func NewFakeClock() *FakeClock

NewFakeClock creates a new FakeClock at the current time.

func (*FakeClock) Advance

func (fc *FakeClock) Advance(d time.Duration)

Advance advances the fake clock by the given duration.

func (*FakeClock) Now

func (fc *FakeClock) Now() time.Time

Now returns the fake current time.

func (*FakeClock) Set

func (fc *FakeClock) Set(t time.Time)

Set sets the fake clock to a specific time.

func (*FakeClock) Sub

func (fc *FakeClock) Sub(t time.Time) time.Duration

Sub returns the duration from the fake now to the given time.

type FakeHandler

type FakeHandler struct {
	Requests  []*http.Request
	Responses []FakeResponse
	// contains filtered or unexported fields
}

FakeHandler records HTTP requests for testing.

func FakeHTTPHandler

func FakeHTTPHandler() (*FakeHandler, http.Handler)

FakeHTTPHandler returns a handler that records requests.

func (*FakeHandler) AssertMethod

func (h *FakeHandler) AssertMethod(method string) bool

AssertMethod asserts that a specific HTTP method was used.

func (*FakeHandler) AssertSent

func (h *FakeHandler) AssertSent(url string) bool

AssertSent asserts that a request was sent to a specific URL.

func (*FakeHandler) LastRequest

func (h *FakeHandler) LastRequest() *http.Request

LastRequest returns the last recorded request.

func (*FakeHandler) Queue

func (h *FakeHandler) Queue(responses ...FakeResponse)

Queue records requests made to the fake handler.

func (*FakeHandler) RequestCount

func (h *FakeHandler) RequestCount() int

RequestCount returns the number of requests made.

func (*FakeHandler) Reset

func (h *FakeHandler) Reset()

Reset clears all recorded requests and responses.

func (*FakeHandler) ServeHTTP

func (h *FakeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP records the request and returns a configured response.

type FakeResponse

type FakeResponse struct {
	StatusCode int
	Body       string
	Headers    map[string]string
}

FakeResponse represents a pre-configured response.

type Mock

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

Mock provides a simple mock framework for testing.

func NewMock

func NewMock() *Mock

NewMock creates a new Mock instance.

func (*Mock) Call

func (m *Mock) Call(method string, args ...any) []any

Call records a method call and returns stubbed values.

func (*Mock) CallHistory

func (m *Mock) CallHistory() []MockCall

CallHistory returns all recorded calls.

func (*Mock) CalledTimes

func (m *Mock) CalledTimes(method string) int

CalledTimes returns the number of times a method was called.

func (*Mock) CalledWith

func (m *Mock) CalledWith(method string, args ...any) bool

CalledWith checks if a method was called with specific arguments.

func (*Mock) LastCall

func (m *Mock) LastCall(method string) *MockCall

LastCall returns the last call for a method.

func (*Mock) Reset

func (m *Mock) Reset()

Reset clears all recorded calls.

func (*Mock) Stub

func (m *Mock) Stub(method string, returns ...any) *Mock

Stub stubs a method to return specific values.

func (*Mock) WasCalled

func (m *Mock) WasCalled(method string) bool

WasCalled checks if a method was called at all.

type MockCall

type MockCall struct {
	Method string
	Args   []any
	Return []any
}

MockCall represents a recorded method call.

type MockStub

type MockStub struct {
	Returns []any
}

MockStub represents a stubbed method return value.

type ParallelTestCase

type ParallelTestCase struct {
	*testing.T
	// contains filtered or unexported fields
}

ParallelTestCase provides parallel test execution support.

func NewParallelTestCase

func NewParallelTestCase(t *testing.T, workers int) *ParallelTestCase

NewParallelTestCase creates a new parallel test case.

func (*ParallelTestCase) Run

func (ptc *ParallelTestCase) Run(fn func(workerID int))

Run runs the given function in parallel with multiple workers.

func (*ParallelTestCase) RunParallel

func (ptc *ParallelTestCase) RunParallel(fns ...func(workerID int))

RunParallel runs multiple test functions in parallel.

type Spy

type Spy struct {
	*Mock
	// contains filtered or unexported fields
}

Spy wraps a real implementation and records calls.

func NewSpy

func NewSpy(real any) *Spy

NewSpy creates a new Spy wrapping a real implementation.

type TestCase

type TestCase struct {
	*testing.T
	App    *foundation.Application
	DB     *orm.DB
	Router *routing.Router
	// contains filtered or unexported fields
}

TestCase provides a fluent testing API similar to Laravel.

func NewTestCase

func NewTestCase(t *testing.T, app *foundation.Application, db *orm.DB, router *routing.Router) *TestCase

NewTestCase initializes a test environment.

func (*TestCase) ActingAs

func (tc *TestCase) ActingAs(user any) *TestCase

ActingAs sets the authenticated user for subsequent test requests.

func (*TestCase) AssertDatabaseCount

func (tc *TestCase) AssertDatabaseCount(table string, expected int)

AssertDatabaseCount asserts that a table has exactly `expected` number of rows.

func (*TestCase) AssertDatabaseHas

func (tc *TestCase) AssertDatabaseHas(table string, conditions map[string]any)

AssertDatabaseHas asserts that a database table contains a row matching the given constraints.

func (*TestCase) AssertDatabaseHasColumns

func (tc *TestCase) AssertDatabaseHasColumns(table string, columns ...string)

AssertDatabaseHasColumns asserts that a table has the given columns.

func (*TestCase) AssertDatabaseHasExactly

func (tc *TestCase) AssertDatabaseHasExactly(table string, conditions map[string]any)

AssertDatabaseHasExactly asserts that a table contains **exactly one** row matching the conditions.

func (*TestCase) AssertDatabaseHasNoRecords

func (tc *TestCase) AssertDatabaseHasNoRecords(table string)

AssertDatabaseHasNoRecords asserts that a table is completely empty.

func (*TestCase) AssertDatabaseMissing

func (tc *TestCase) AssertDatabaseMissing(table string, conditions map[string]any)

AssertDatabaseMissing asserts that a database table does NOT contain a row matching the given constraints.

func (*TestCase) AssertDatabaseTable

func (tc *TestCase) AssertDatabaseTable(table string)

AssertDatabaseTable asserts that a database table exists.

func (*TestCase) AssertNotSoftDeleted

func (tc *TestCase) AssertNotSoftDeleted(table string, conditions map[string]any)

AssertNotSoftDeleted asserts that a row has NOT been soft-deleted.

func (*TestCase) AssertSoftDeleted

func (tc *TestCase) AssertSoftDeleted(table string, conditions map[string]any)

AssertSoftDeleted asserts that a row has been soft-deleted (deleted_at is not null).

func (*TestCase) Get

func (tc *TestCase) Get(uri string) *TestResponse

Get dispatch a GET request to the application.

func (*TestCase) Post

func (tc *TestCase) Post(uri string, body io.Reader) *TestResponse

Post dispatch a POST request to the application.

func (*TestCase) Upload

func (tc *TestCase) Upload(url, fieldName, filename, content string) *TestResponse

Upload simulates a file upload (multipart).

type TestResponse

type TestResponse struct {
	T        *testing.T
	Recorder *httptest.ResponseRecorder
}

TestResponse wraps the httptest.ResponseRecorder for fluent assertions.

func (*TestResponse) AssertClientError

func (tr *TestResponse) AssertClientError() *TestResponse

AssertClientError asserts the response status is 4xx.

func (*TestResponse) AssertCookie

func (tr *TestResponse) AssertCookie(name string) *TestResponse

AssertCookie asserts the response contains a cookie with the given name.

func (*TestResponse) AssertCookieExists

func (tr *TestResponse) AssertCookieExists(name string) *TestResponse

AssertCookieExists asserts a cookie exists

func (*TestResponse) AssertCookieExpired

func (tr *TestResponse) AssertCookieExpired(name string) *TestResponse

AssertCookieExpired asserts a cookie is expired

func (*TestResponse) AssertCookieNotExpired

func (tr *TestResponse) AssertCookieNotExpired(name string) *TestResponse

AssertCookieNotExpired asserts a cookie is not expired

func (*TestResponse) AssertCookieValue

func (tr *TestResponse) AssertCookieValue(name, value string) *TestResponse

AssertCookieValue asserts the response contains a cookie with the given value.

func (*TestResponse) AssertCreated

func (tr *TestResponse) AssertCreated() *TestResponse

AssertCreated asserts a 201 status

func (*TestResponse) AssertDontSee

func (tr *TestResponse) AssertDontSee(text string) *TestResponse

AssertDontSee asserts the response body does NOT contain the given text.

func (*TestResponse) AssertDontSeeHtml

func (tr *TestResponse) AssertDontSeeHtml(html string) *TestResponse

AssertDontSeeHtml asserts the response doesn't contain HTML

func (*TestResponse) AssertDownload

func (tr *TestResponse) AssertDownload(filename string) *TestResponse

AssertDownload asserts the response is a file download

func (*TestResponse) AssertExactJson

func (tr *TestResponse) AssertExactJson(data map[string]any) *TestResponse

AssertExactJson asserts the response contains exact JSON

func (*TestResponse) AssertForbidden

func (tr *TestResponse) AssertForbidden() *TestResponse

AssertForbidden asserts a 403 status

func (*TestResponse) AssertHeader

func (tr *TestResponse) AssertHeader(key, value string) *TestResponse

AssertHeader asserts the response contains a specific header value.

func (*TestResponse) AssertHeaderContains

func (tr *TestResponse) AssertHeaderContains(name, value string) *TestResponse

AssertHeaderContains asserts a header contains a value

func (*TestResponse) AssertIsRedirect

func (tr *TestResponse) AssertIsRedirect() *TestResponse

AssertIsRedirect asserts a 3xx redirect

func (*TestResponse) AssertJson

func (tr *TestResponse) AssertJson(key string, value any) *TestResponse

AssertJson asserts the response contains the given JSON key/value.

func (*TestResponse) AssertJsonCount

func (tr *TestResponse) AssertJsonCount(key string, count int) *TestResponse

AssertJsonCount asserts the response JSON array has the given count.

func (*TestResponse) AssertJsonMap

func (tr *TestResponse) AssertJsonMap(expected map[string]any) *TestResponse

AssertJsonMap asserts the response JSON matches the given map.

func (*TestResponse) AssertJsonStructure

func (tr *TestResponse) AssertJsonStructure(keys ...string) *TestResponse

AssertJsonStructure asserts top-level keys exist.

func (*TestResponse) AssertJsonValidationErrors

func (tr *TestResponse) AssertJsonValidationErrors(fields ...string) *TestResponse

AssertJsonValidationErrors asserts JSON validation errors for fields

func (*TestResponse) AssertNotFound

func (tr *TestResponse) AssertNotFound() *TestResponse

AssertNotFound asserts a 404 status

func (*TestResponse) AssertOk

func (tr *TestResponse) AssertOk() *TestResponse

AssertOk asserts the response status is 200 OK.

func (*TestResponse) AssertRedirect

func (tr *TestResponse) AssertRedirect(url string) *TestResponse

AssertRedirect asserts a redirect response

func (*TestResponse) AssertRedirectToRoute

func (tr *TestResponse) AssertRedirectToRoute(route string, params ...any) *TestResponse

AssertRedirectToRoute asserts a redirect to a named route

func (*TestResponse) AssertSee

func (tr *TestResponse) AssertSee(text string) *TestResponse

AssertSee asserts the response body contains the given text.

func (*TestResponse) AssertSeeHtml

func (tr *TestResponse) AssertSeeHtml(html string) *TestResponse

AssertSeeHtml asserts the response contains HTML

func (*TestResponse) AssertSeeText

func (tr *TestResponse) AssertSeeText(text string) *TestResponse

AssertSeeText asserts the response body contains the given text (as plain text).

func (*TestResponse) AssertServerError

func (tr *TestResponse) AssertServerError() *TestResponse

AssertServerError asserts the response status is 5xx.

func (*TestResponse) AssertStatus

func (tr *TestResponse) AssertStatus(code int) *TestResponse

AssertStatus asserts the response HTTP status code.

func (*TestResponse) AssertStreamed

func (tr *TestResponse) AssertStreamed() *TestResponse

AssertStreamed asserts the response is streamed

func (*TestResponse) AssertSuccessful

func (tr *TestResponse) AssertSuccessful() *TestResponse

AssertSuccessful asserts the response status is 2xx.

func (*TestResponse) AssertUnauthorized

func (tr *TestResponse) AssertUnauthorized() *TestResponse

AssertUnauthorized asserts a 401 status

func (*TestResponse) AssertUnprocessableEntity

func (tr *TestResponse) AssertUnprocessableEntity() *TestResponse

AssertUnprocessableEntity asserts a 422 status

func (*TestResponse) AssertViewHas

func (tr *TestResponse) AssertViewHas(key string) *TestResponse

AssertViewHas asserts the view data contains the given key.

func (*TestResponse) AssertViewHasValue

func (tr *TestResponse) AssertViewHasValue(key string, value string) *TestResponse

AssertViewHasValue asserts the view data contains the given key with the expected value.

func (*TestResponse) AssertViewIs

func (tr *TestResponse) AssertViewIs(view string) *TestResponse

AssertViewIs asserts the response was rendered with the given view name.

type TestResult

type TestResult struct {
	WorkerID int
	Duration time.Duration
	Error    error
}

TestResult represents the result of a concurrent test.

type TimeTravel

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

TimeTravel provides time manipulation for testing.

func FreezeTime

func FreezeTime() *TimeTravel

FreezeTime freezes the global time.

func GetTimeTravel

func GetTimeTravel() *TimeTravel

GetTimeTravel returns the global TimeTravel instance.

func NewTimeTravel

func NewTimeTravel() *TimeTravel

NewTimeTravel creates a new TimeTravel instance.

func TravelTime

func TravelTime(d time.Duration) *TimeTravel

TravelTime travels by the given duration using the global time.

func (*TimeTravel) Add

func (t *TimeTravel) Add(d time.Duration)

Add adds duration to the current fake time.

func (*TimeTravel) Freeze

func (t *TimeTravel) Freeze()

Freeze freezes the current time.

func (*TimeTravel) IsFrozen

func (t *TimeTravel) IsFrozen() bool

IsFrozen returns whether time travel is active.

func (*TimeTravel) Now

func (t *TimeTravel) Now() time.Time

Now returns the current "fake" time.

func (*TimeTravel) SetNow

func (t *TimeTravel) SetNow(now time.Time)

SetNow sets the current fake time.

func (*TimeTravel) Since

func (t *TimeTravel) Since(then time.Time) time.Duration

Since returns the duration since the given time (using fake time).

func (*TimeTravel) Sub

func (t *TimeTravel) Sub(other time.Time) time.Duration

Sub returns the difference between two fake times.

func (*TimeTravel) Travel

func (t *TimeTravel) Travel(d time.Duration)

Travel moves the fake time by the given duration.

func (*TimeTravel) TravelToDate

func (t *TimeTravel) TravelToDate(year int, month time.Month, day, hour, min, sec int)

TravelToDate moves the fake time to a specific date.

func (*TimeTravel) Unfreeze

func (t *TimeTravel) Unfreeze()

Unfreeze stops time travel and returns to real time.

func (*TimeTravel) Until

func (t *TimeTravel) Until(then time.Time) time.Duration

Until returns the duration until the given time (using fake time).

Jump to

Keyboard shortcuts

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