2017-04-19 1 views
4

Mon dossier de projet contient:Comment exécuter aller test sur tous les fichiers de test dans mon projet, sauf pour les packages de fournisseurs

Makefile README.md component/ driver/ service/ vendor/ worker/ 

Je voudrais courir go test sur tous les fichiers de test, par exemple foobar_test.go à l'exception des fichiers de test dans le package fournisseur. Le plus proche que je suis arrivé au succès était avec go test ./... mais cela incluait des fichiers de test de fournisseur.

J'ai vu dans la documentation que vous pouvez passer une option regex à -run mais j'ai du mal à faire fonctionner ça. Par exemple, j'ai essayé go test ./*, mais je reçois un tas de can't load package errors.

Quelle est la meilleure façon de faire cela?

Répondre

13

Le modèle -run correspond uniquement à l'identificateur de test (pas le nom de fichier); En principe, vous pouvez faire:

go test -run TestFoo 

mais quand vous auriez à ajouter Foo à tous vos noms de fonctions de test, que vous ne voulez probablement pas.

La manière habituelle de faire est la suivante:

go test $(go list ./... | grep -v /vendor/) 

Il y a a lengthy discussion at GitHub about it. Il a finalement été changé after another lengthy discussion. Le répertoire commençant par Go 1.9, vendor est automatiquement exclu. Maintenant, vous pouvez taper directement ceci:

go test ./... 
2

cmd/go: exclude vendor dir from matching ... #19090

[go] cmd/go: exclude vendored packages from ... matches

By overwhelming popular demand, exclude vendored packages from ... matches, 
by making ... never match the "vendor" element above a vendored package. 

go help packages now reads: 

    An import path is a pattern if it includes one or more "..." wildcards, 
    each of which can match any string, including the empty string and 
    strings containing slashes. Such a pattern expands to all package 
    directories found in the GOPATH trees with names matching the 
    patterns. 

    To make common patterns more convenient, there are two special cases. 
    First, /... at the end of the pattern can match an empty string, 
    so that net/... matches both net and packages in its subdirectories, like net/http. 
    Second, any slash-separted pattern element containing a wildcard never 
    participates in a match of the "vendor" element in the path of a vendored 
    package, so that ./... does not match packages in subdirectories of 
    ./vendor or ./mycode/vendor, but ./vendor/... and ./mycode/vendor/... do. 
    Note, however, that a directory named vendor that itself contains code 
    is not a vendored package: cmd/vendor would be a command named vendor, 
    and the pattern cmd/... matches it. 

Fixes #19090. 

go/go/fa1d54c2edad607866445577fe4949fbe55166e1

commit fa1d54c2edad607866445577fe4949fbe55166e1 
Wed Mar 29 18:51:44 2017 +0000 

Essayez d'exécuter go test ./... au bout ou attendez Go1.9.