All Projects → adams-sarah → Test2doc

adams-sarah / Test2doc

Licence: mit
Generate documentation for your REST/HTTP API from your Go unit tests

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Test2doc

newdoc
The newdoc tool generates files formatted with AsciiDoc, which are used in Red Hat documentation.
Stars: ✭ 14 (-95.72%)
Mutual labels:  documentation-generator
doctave
A batteries-included developer documentation site generator
Stars: ✭ 349 (+6.73%)
Mutual labels:  documentation-generator
Pydoc Markdown
Create Python API documentation in Markdown format.
Stars: ✭ 273 (-16.51%)
Mutual labels:  documentation-generator
treblle-laravel
The offical Treblle package for Laravel
Stars: ✭ 43 (-86.85%)
Mutual labels:  documentation-generator
reslate
Beautiful static documentation for your API
Stars: ✭ 98 (-70.03%)
Mutual labels:  documentation-generator
literator
📝 Generate literate-style markdown docs from your sources
Stars: ✭ 55 (-83.18%)
Mutual labels:  documentation-generator
live-documenter
.NET documentation generator and live reader. Generate documentation from .NET code and xml comments, fast, quick and easy.
Stars: ✭ 64 (-80.43%)
Mutual labels:  documentation-generator
Insomnia Documenter
Tool to create minimalist and beautiful API documentation pages using your Insomnia workspace export file.
Stars: ✭ 284 (-13.15%)
Mutual labels:  documentation-generator
doxybook2
Doxygen XML to Markdown (or JSON)
Stars: ✭ 140 (-57.19%)
Mutual labels:  documentation-generator
Doxyrest
A compiler from Doxygen XML to reStructuredText -- hence, the name. It parses XML databases generated by Doxygen and produces reStructuredText for the Python documentation generator Sphinx.
Stars: ✭ 265 (-18.96%)
Mutual labels:  documentation-generator
tygen
Modern documentation generator for TypeScript built with ReactJS
Stars: ✭ 23 (-92.97%)
Mutual labels:  documentation-generator
hubi
Humanitarian ubiquitous language helper
Stars: ✭ 17 (-94.8%)
Mutual labels:  documentation-generator
asciidoctor-lein-plugin
A Leiningen plugin for generating documentation using Asciidoctor
Stars: ✭ 26 (-92.05%)
Mutual labels:  documentation-generator
badge-generator
Magically generate Markdown badges for your docs 🛡️ 🦡 🧙
Stars: ✭ 104 (-68.2%)
Mutual labels:  documentation-generator
Literate.jl
Simple package for literate programming in Julia
Stars: ✭ 272 (-16.82%)
Mutual labels:  documentation-generator
ModuleInterface
Swift tool to generate Module Interfaces for Swift projects.
Stars: ✭ 70 (-78.59%)
Mutual labels:  documentation-generator
vitepress-for-component
📖 针对组件开发的VitePress。
Stars: ✭ 142 (-56.57%)
Mutual labels:  documentation-generator
Serverless Aws Documentation
Serverless 1.0 plugin to add documentation and models to the serverless generated API Gateway
Stars: ✭ 299 (-8.56%)
Mutual labels:  documentation-generator
Sourcedocs
Generate Markdown documentation from source code
Stars: ✭ 286 (-12.54%)
Mutual labels:  documentation-generator
insomnia-plugin-documenter
Export Insomnia workspace HTML documentation.
Stars: ✭ 23 (-92.97%)
Mutual labels:  documentation-generator

test2doc

wercker status

Generate documentation for your REST/HTTP API from your Go unit tests - a simple addition to Go's testing package.

Diving right in..

Given a handler func:

// GetWidget retrieves a single Widget
func GetWidget(w http.ResponseWriter, req *http.Request) {
	// get widget
	// respond with widget JSON
	// ...
}

And a test for this handler func:

func TestGetWidget(t *testing.T) {
	urlPath := fmt.Sprintf("/widgets/%d", 2)

	resp, err := http.Get(server.URL + urlPath)
	// assert all the things...
}

Test2doc will generate markdown documentation for this endpoint in the API Blueprint format, like so:

# Group widgets

## /widgets/{id}

+ Parameters
    + id: `2` (number)

### Get Widget [GET]
retrieves a single Widget

+ Response 200 

    + Body

            {
                "Id": 2,
                "Name": "Pencil",
                "Role": "Utensil"
            }        

Which you can then parse and host w/ Apiary.io, eg here. Or use a custom parser and host yourself.

screenshot


Things to note:

  1. Go pkg name becomes Group name
  2. Go handler name becomes endpoint title
  3. Go handler godoc string becomes endpoint description
  4. Everything else is recorded & interpreted directly from the requests and responses

Eg.

package widget

// GetWidget retrieves a single Widget
func GetWidget(w http.ResponseWriter, req *http.Request)

becomes

# Group widget

### Get Widget [*]
retrieves a single Widget

Installation

go get github.com/adams-sarah/test2doc/...


Integrating test2doc

Very few additions, and only to your testing code.

1. Add 3 things to your TestMain:


import (
	"github.com/adams-sarah/test2doc/test"
)

var server *test.Server

func TestMain(m *testing.M) {
	// 1. Tell test2doc how to get URL vars out of your HTTP requests
	//
	//    The 'URLVarExtractor' function must have the following signature:
	//      func(req *http.Request) map[string]string
	//      where the returned map is of the form map[key]value
	test.RegisterURLVarExtractor(myURLVarExtractorFn)


	// 2. You must use test2doc/test's wrapped httptest.Server instead of
	//    the raw httptest.Server, so that test2doc can listen to and
	//    record requests & responses.
	//
	//    NewServer takes your HTTP handler as an argument
	server, err := test.NewServer(router)
	if err != nil {
		panic(err.Error())
	}

	// .. then run your tests as usual
	// (remember that os.Exit does not respect defers)
	exitCode := m.Run()


	// 3. Finally, you must tell the wrapped server when you are done testing
	//    so that the buffer can be flushed to an apib doc file
	server.Finish()

	// note that os.Exit does not respect defers.
	os.Exit(exitCode)
}

Router-specific configurations

gorilla/mux configurations

	import "github.com/adams-sarah/test2doc/vars"
	// ...
	
	extractor := vars.MakeGorillaMuxExtractor(myGorillaRouter)
	test.RegisterURLVarExtractor(extractor)

julienschmidt/httprouter configurations

	import "github.com/adams-sarah/test2doc/vars"
	// ...

	extractor := vars.MakeHTTPRouterExtractor(myHTTPRouter)
	test.RegisterURLVarExtractor(extractor)

2. Combine the output .apib files

test2doc will spit out one doc file per package.

Eg. A package tree like:

.
├── foos
│   ├── foos.go
│   └── foos_test.go
└── widgets
    ├── widgets.go
    └── widgets_test.go

Will produce separate apib files, eg:

.
├── foos
│   ├── ...
│   └── foos.apib
└── widgets
    ├── ...
    └── widgets.apib

You will need to add the doc header (below) and combine all of the package doc files after your tests run.

eg.:

# find all *.apib files (after tests have run, generated files)
files=`find . -type f -name "*.apib"`

# copy template file to new apiary.apib file
cp apib.tmpl apiary.apib

# copy contents of each generated apib file into apiary.apib
# and delete the apib file
for f in ${files[@]}; do
	cat $f >> apiary.apib
	rm $f
done

where apib.tmpl includes the doc header information. Something like:

FORMAT: 1A
HOST: https://api.mysite.com

# The API for My Site

My Site is a fancy site. The API is also fancy.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].