Postman cli newman 📋 Copia tutti i comandi_HTML_TAG_89__ 📄 Generare PDF__HTML_TAG_91_ Traduzione: # Postman CLI (newman) Cheatsheet ## Installazione _Tabella_99_ # Comandi di base _Tabella_100__ ## Uso avanzato _Tabella_101__ ## Configurazione ### Ambiente Struttura dei file { "id": "environment-id", "name": "Production Environment", "values": [ { "key": "base_url", "value": "https://api.production.com", "enabled": true }, { "key": "api_key", "value": "secret-key-123", "enabled": true, "type": "secret" }, { "key": "timeout", "value": "5000", "enabled": true } ] } ### Global Variables File { "id": "globals-id", "name": "Global Variables", "values": [ { "key": "company_name", "value": "Acme Corp", "enabled": true }, { "key": "api_version", "value": "v1", "enabled": true } ] } ### Newman Configuration File (newman.json) { "collection": "collection.json", "environment": "environment.json", "globals": "globals.json", "iterationCount": 5, "delayRequest": 1000, "timeoutRequest": 10000, "reporters": ["cli", "html", "json"], "reporter": { "html": { "export": "./reports/report.html", "template": "./custom-template.hbs" }, "json": { "export": "./reports/report.json" } }, "bail": false, "insecure": false, "color": "on" } # File dati per iterazioni (CSV) username,password,expected_status user1,pass123,200 user2,pass456,200 admin,admin789,200 invalid,wrong,401 # File dati per iterazioni (JSON) [ { "username": "user1", "password": "pass123", "expected_status": 200 }, { "username": "user2", "password": "pass456", "expected_status": 200 } ] ### Opzioni Reporter # HTML Reporter with all options newman run collection.json \ -r html \ --reporter-html-export ./reports/report.html \ --reporter-html-template ./template.hbs \ --reporter-html-title "API Test Report" # JSON Reporter newman run collection.json \ -r json \ --reporter-json-export ./reports/output.json # JUnit Reporter newman run collection.json \ -r junit \ --reporter-junit-export ./reports/junit.xml # CLI Reporter (suppress specific outputs) newman run collection.json \ -r cli \ --reporter-cli-no-summary \ --reporter-cli-no-failures ## Common Use Cases ### Use Case 1: Integrazione CI/CD Pipeline #!/bin/bash # Jenkins/GitLab CI script for API testing # Run collection with environment newman run api-tests.json \ -e production.json \ -r cli,junit \ --reporter-junit-export ./test-results/junit.xml \ --bail \ --suppress-exit-code # Check exit code if [ $? -eq 0 ]; then echo "All tests passed" exit 0 else echo "Tests failed" exit 1 fi ### Use Case 2: Data-Driven Testing # Test multiple user scenarios from CSV newman run user-registration.json \ -d user-data.csv \ -e staging.json \ -r html,cli \ --reporter-html-export ./reports/registration-tests.html \ --delay-request 500 \ --timeout-request 15000 ### Use Case 3: Test multi-ambiente #!/bin/bash # Test across multiple environments ENVIRONMENTS=("dev" "staging" "production") for env in "${ENVIRONMENTS[@]}"; do echo "Testing $env environment..." newman run collection.json \ -e "environments/${env}.json" \ -r html \ --reporter-html-export "./reports/${env}-report.html" \ --bail if [ $? -ne 0 ]; then echo "Tests failed in $env environment" exit 1 fi done echo "All environments tested successfully" ### Use Case 4: Calendario API programmato #!/bin/bash # Cron job for API health monitoring (run every 15 minutes) # */15 * * * * /path/to/api-monitor.sh DATE=$(date +%Y%m%d-%H%M%S) newman run health-check.json \ -e production.json \ -r json,html \ --reporter-json-export "./logs/health-${DATE}.json" \ --reporter-html-export "./logs/health-${DATE}.html" \ --timeout-request 5000 \ --suppress-exit-code # Send alert if tests fail if [ $? -ne 0 ]; then echo "API health check failed at ${DATE}" | mail -s "API Alert" admin@example.com fi ### Use Case 5: Esecuzione programmatica (Node.js) // test-runner.js const newman = require('newman'); newman.run({ collection: require('./collection.json'), environment: require('./environment.json'), reporters: ['cli', 'html', 'json'], reporter: { html: { export: './reports/report.html' }, json: { export: './reports/report.json' } }, iterationCount: 3, delayRequest: 500, timeout: 10000 }, function (err, summary) { if (err) { console.error('Collection run failed:', err); process.exit(1); } console.log('Collection run complete!'); console.log('Total requests:', summary.run.stats.requests.total); console.log('Failed requests:', summary.run.stats.requests.failed); console.log('Test failures:', summary.run.stats.tests.failed); // Exit with error code if tests failed if (summary.run.stats.tests.failed > 0) { process.exit(1); } }); # Migliori Pratiche - **Utilizzare le variabili ambientali**: memorizzare i dati sensibili (chiavi API, password) nei file ambientali, mai hardcode nelle collezioni - **Implement Proper Timeouts**: Impostare valori realistici `--timeout-request` sulla base dei tempi di risposta previsti dall'API per evitare errori falsi - **Abilita reportistica dettagliata**: Utilizzare più reporter (`-r cli,html,junit`) per risultati di test completi e integrazione CI/CD - **Version Control Your Collections**: Esporta collezioni da Postman e salva in Git accanto al tuo codebase per il tracciamento delle versioni - **Utilizzare i file di dati per scalabilità**: Leverage CSV/JSON file di dati con la bandiera `-d`_ per testare scenari multipli senza duplicare richieste - **Implement Retry Logic**: Per i test infuocati, avvolgere i comandi di newman in loop di riprovazione o utilizzare i meccanismi di riprovazione CI/CD - **Organizzare Collezioni per scopo**: Test di fumo separati, test di regressione e test di integrazione in diverse collezioni per esecuzione mirata - ** Risultati dell'esportazione e dell'archivio**: Utilizzare sempre `--export-environment` e `--export-globals` per catturare lo stato di runtime per il debugging - **Secure SSL Certificates**: Utilizzare `--ssl-client-cert` e `--ssl-client-key`_ per autenticare il test API, evitare `--insecure`___ nella produzione - **Monitor Performance**: Traccia i tempi di esecuzione nei report per identificare i punti di estremità lenti e il potenziale degrado delle prestazioni ## Risoluzione dei problemi _Tabella_102__ ## Risorse aggiuntive ### Exporting Collections from Postman # Using Postman API to export collection curl -X GET \ 'https://api.getpostman.com/collections/<collection-uid>' \ -H 'X-Api-Key: <your-postman-api-key>' \ -o collection.json # Run exported collection newman run collection.json ## Installazione di Newman Reporters # HTML reporter (included by default) npm install -g newman-reporter-html # HTMLExtra reporter (enhanced HTML reports) npm install -g newman-reporter-htmlextra # Confluence reporter npm install -g newman-reporter-confluence # Slack reporter npm install -g newman-reporter-slack # TeamCity reporter npm install -g newman-reporter-teamcity ### # Using Custom Reporters # Run with custom reporter newman run collection.json -r htmlextra # HTMLExtra with options newman run collection.json \ -r htmlextra \ --reporter-htmlextra-export ./report.html \ --reporter-htmlextra-darkTheme \ --reporter-htmlextra-title "Custom API Tests" ### Modello di esecuzione parallela # Using GNU Parallel for concurrent execution parallel -j 4 newman run ::: \ auth-tests.json \ user-tests.json \ order-tests.json \ payment-tests.json # Using background jobs newman run collection1.json -e env1.json & \ newman run collection2.json -e env2.json & \ newman run collection3.json -e env3.json & \ wait echo "All collections completed"