Si vous créez une fonction, consultez le guide de démarrage rapide de la console sur Cloud Run. Le contenu de cette page ne s'applique qu'aux anciennes fonctions existantes créées avec l'API Cloud Functions v1.
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Tester une fonction Cloud Run avant le déploiement
Ce guide vous explique comment tester une fonction Cloud Run dans la console Google Cloud avant de la déployer. Cette fonctionnalité est disponible pour les fonctions Node.js et Python.
Préparer une fonction à des tests
Pour préparer une fonction à des tests :
Ouvrez la page de présentation de Cloud Functions dans la console Google Cloud :
Vérifiez que le projet pour lequel vous avez activé Cloud Run Functions est sélectionné.
Cliquez sur Créer une fonction pour créer une fonction Node.js ou Python, ou sélectionnez une fonction Node.js ou Python existante, puis cliquez sur Modifier pour la modifier.
Cliquez sur Suivant pour accéder à la page de modification du code :
Cliquez sur Tester la fonction pour lancer le processus de test.
Lorsque vous y êtes invité, cliquez sur Démarrer les tests pour autoriser l'utilisation du terminal Cloud Shell.
Exécuter le test
Une fois la procédure ci-dessus terminée, le panneau des événements déclencheurs s'ouvre sur la droite, confirmant que la fonction est prête à être testée. Cliquez sur Run test (Exécuter le test) :
Cette phase du processus de test comprend les composants suivants :
Panneau "Événement déclencheur" Le panneau d'événement déclencheur est prérempli avec une charge utile correspondant au déclencheur d'événement que vous avez spécifié sur la page de configuration. Il s'agit de l'entrée de votre fonction.
Fenêtre de sortie Cloud Shell. La fenêtre de sortie Cloud Shell affiche le code d'état HTTP et toute réponse renvoyée à l'appelant.
L'option Exécuter le test exécute le test.
Vous pouvez observer le résultat du test dans la fenêtre Cloud Shell :
Terminal Cloud Shell
Vous pouvez afficher la sortie de journalisation de votre fonction dans le terminal Cloud Shell en bas de l'écran. Par exemple, vous verrez le résultat de l'empaquetage du code source lorsque vous ouvrirez le panneau de test. Lorsque vous exécutez un test, vous voyez les modifications du code source et le résultat du niveau d'exécution, y compris console.log et les erreurs.
Si votre test échoue, le terminal Cloud Shell affiche un message d'erreur pour vous aider à identifier la cause exacte. Exemple :
[11:31:47 AM] - Provided module can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /workspace/index.js:11
: 'World';
^
SyntaxError: Unexpected token ':'
at Object.compileFunction (node:vm:360:18)
at wrapSafe (node:internal/modules/cjs/loader:1048:15)
at Module._compile (node:internal/modules/cjs/loader:1083:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
at Module.load (node:internal/modules/cjs/loader:997:32)
at Module._load (node:internal/modules/cjs/loader:838:12)
at Module.require (node:internal/modules/cjs/loader:1021:19)
at require (node:internal/modules/cjs/helpers:103:18)
at getUserFunction (/workspace/node_modules/@google-cloud/functions-framework/build/src/loader.js:98:30)
at async main (/workspace/node_modules/@google-cloud/functions-framework/build/src/main.js:35:32)
[11:31:47 AM] - Could not load the function, shutting down.
Déployer
Une fois les tests terminés, vous pouvez déployer votre fonction de la manière habituelle en cliquant sur Deploy (Déployer).
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/03 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/03 (UTC)."],[[["\u003cp\u003eThis guide demonstrates how to test Node.js and Python Cloud Run functions within the Google Cloud console before deployment, ensuring functionality.\u003c/p\u003e\n"],["\u003cp\u003eThe testing process includes a triggering event panel with pre-populated payload, a Cloud Shell output window for HTTP status and responses, and a "Run test" button to execute the test.\u003c/p\u003e\n"],["\u003cp\u003eCloud Shell terminal logs provide detailed output, showing source code packaging and execution data, including \u003ccode\u003econsole.log\u003c/code\u003e outputs and potential error messages to aid in debugging.\u003c/p\u003e\n"],["\u003cp\u003eIf tests fail, the Cloud Shell terminal displays specific error messages to help pinpoint issues, suggesting code review and test re-runs or a complete restart if the problem persists.\u003c/p\u003e\n"],["\u003cp\u003eThe Cloud Run functions testing is currently under a "Pre-GA Offerings Terms", meaning that the feature is "as-is" with potential limited support.\u003c/p\u003e\n"]]],[],null,["# Test a Cloud Run functions before deployment\n============================================\n\n|\n| **Preview**\n|\n|\n| This product or feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA products and features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\nThis guide shows you how to test a Cloud Run function in Google Cloud console\nbefore deploying it. This feature is available for Node.js and Python functions.\n\nPrepare a function for testing\n------------------------------\n\nTo prepare a function for testing:\n\n1. Open the Functions Overview page in the Google Cloud console:\n\n [Go to the Cloud Run functions Overview page](https://console.cloud.google.com/functions/list)\n\n Make sure that the project for which you enabled Cloud Run functions is\n selected.\n2. Click **Create Function** to create a new Node.js or Python function, or\n select an existing Node.js or Python function and click **Edit** to edit it.\n\n3. Click **Next** to advance to the code editing page:\n\n4. Click **Test Function** to start the testing process.\n\n5. When prompted, click **Start testing** to authorize use of the\n Cloud Shell Terminal.\n\nRun the test\n------------\n\nWhen you complete the preceding steps, the triggering event panel opens on the\nright, confirming that the function is ready to be tested. Click **Run test**:\n\nThis phase of the testing process includes the following components:\n\n- **Triggering event panel**. The triggering event panel is pre-populated with a payload that matches the event trigger you specified on the configuration page. This is the input to your function.\n- **Cloud Shell output window**. The Cloud Shell output window displays the HTTP status code and any response returned to the caller.\n- **Run test** executes the test.\n\nYou can observe the output from the test in the Cloud Shell window:\n\n| **Note:** It can take 1-3 minutes when initializing the testing panel for the first time. Subsequent times should only take a few seconds. If it takes longer than 3 minutes and you don't see any progress indicators, back up your source code, refresh your browser tab, and restart testing from scratch.\n\nCloud Shell terminal\n--------------------\n\nYou can view logging output by your function in the Cloud Shell terminal at\nthe bottom of your screen. For example, you'll see source code packaging output\nwhen you open the testing panel. When you run a test, you'll see your source\ncode changes and execution level output, including `console.log` and errors.\n\nIf your test fails, the Cloud Shell terminal displays an error message\nto help you pinpoint the exact cause, for example: \n\n```\n[11:31:47 AM] - Provided module can't be loaded.\nIs there a syntax error in your code?\nDetailed stack trace: /workspace/index.js:11\n : 'World';\n ^\n\nSyntaxError: Unexpected token ':'\n at Object.compileFunction (node:vm:360:18)\n at wrapSafe (node:internal/modules/cjs/loader:1048:15)\n at Module._compile (node:internal/modules/cjs/loader:1083:27)\n at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)\n at Module.load (node:internal/modules/cjs/loader:997:32)\n at Module._load (node:internal/modules/cjs/loader:838:12)\n at Module.require (node:internal/modules/cjs/loader:1021:19)\n at require (node:internal/modules/cjs/helpers:103:18)\n at getUserFunction (/workspace/node_modules/@google-cloud/functions-framework/build/src/loader.js:98:30)\n at async main (/workspace/node_modules/@google-cloud/functions-framework/build/src/main.js:35:32)\n\n[11:31:47 AM] - Could not load the function, shutting down.\n```\n| **Note:** If the server crashes, you will see a message prompting you to check your source code and re-run the test. If that does not fix the problem, back up your source code, refresh your browser tab, and restart testing from scratch.\n\nDeploy\n------\n\nOnce you're finished testing, you can deploy your function in the usual way\nby clicking **Deploy**."]]