Domanda

Sto cercando di incorporare alcuni unit test JavaScript nel mio processo di creazione automatizzata.Attualmente JSUnit funziona bene con JUnit, ma sembra essere abbandonato e manca di un buon supporto per AJAX, debug e timeout.

Qualcuno ha avuto fortuna nell'automatizzare (con ANT) una libreria di test unitari come YUI test, QUnit di JQuery o jQUnit (http://code.google.com/p/jqunit/)?

Nota:Utilizzo una libreria AJAX personalizzata, quindi il problema con DOH di Dojo è che richiede l'utilizzo delle proprie chiamate di funzione AJAX e gestori di eventi per funzionare con qualsiasi unit test AJAX.

È stato utile?

Soluzione

Esistono molti framework di test unitari Javascript (jsUnit, scriptaculous, ...) ma jsUnit è l'unico che conosco che può essere utilizzato con una build automatizzata.

Se stai eseguendo un test unitario "vero" non dovresti aver bisogno del supporto AJAX.Ad esempio, se stai utilizzando un framework Ajax RPC come DWR, puoi facilmente scrivere una funzione fittizia:

   function mockFunction(someArg, callback) {
      var result = ...; // some treatments   
      setTimeout(
function() { callback(result); }, 300 // some fake latency
); }

E sì, JsUnit gestisce i timeout: Simulazione del tempo nei test jsUnit

Altri suggerimenti

Sto per iniziare a eseguire Javascript TDD su un nuovo progetto su cui sto lavorando.Il mio piano attuale è quello di utilizzare qunità per eseguire il test unitario.Durante lo sviluppo i test possono essere eseguiti semplicemente aggiornando la pagina di test in un browser.

Per l'integrazione continua (e per garantire l'esecuzione dei test in tutti i browser), utilizzerò Selenio per caricare automaticamente il test cablaggio in ciascun browser e leggere il risultato.Questi test verranno eseguiti a ogni accesso al controllo del codice sorgente.

Utilizzerò anche io JSCoverage per ottenere l'analisi della copertura del codice dei test.Anche questo sarà automatizzato con Selenium.

Attualmente sono nel bel mezzo della configurazione.Aggiornerò questa risposta con dettagli più precisi una volta completata la configurazione.


Strumenti di test:

Ne sono un grande fan js-test-driver

Funziona bene in un ambiente CI ed è in grado di acquisire browser reali per test su più browser.

Recentemente ho letto un articolo di Bruno che utilizza JsUnit e crea un framework JsMock sopra a quello...molto interessante.Sto pensando di utilizzare il suo lavoro per iniziare a testare il mio codice Javascript.

Mock Javascript o Come eseguire test unitari di Javascript al di fuori dell'ambiente del browser

semplicemente ho convinto Hudson CI a eseguire JasmineBDD (senza testa), almeno per i test unitari javascript puri.

(Hudson esegue Java tramite shell, esegue Envjs, esegue JasmineBDD.)

Tuttavia, non sono ancora riuscito a farlo funzionare bene con una grande libreria, come il prototipo.

Guardare dentro YUITest

Sono d'accordo sul fatto che jsunit stia morendo sul nascere.Abbiamo appena finito di sostituirlo con YUI Test.

Similmente all'esempio utilizzando qUnit, stiamo eseguendo i test utilizzando Selenium.Stiamo eseguendo questo test indipendentemente dagli altri nostri test sul selenio semplicemente perché non ha le dipendenze che hanno i normali test di regressione dell'interfaccia utente (ad es.distribuzione dell'app su un server).

Per iniziare, abbiamo un file javascript di base incluso in tutti i nostri file html di prova.Questo gestisce la configurazione dell'istanza YUI, del test runner, dell'oggetto YUI.Test.Suite e del Test.Case.Ha un metodo a cui è possibile accedere tramite Selenium per eseguire la suite di test, verificare se il test runner è ancora in esecuzione (i risultati non sono disponibili fino al termine) e ottenere i risultati del test (abbiamo scelto il formato JSON)

var yui_instance; //the YUI instance
var runner;  //The YAHOO.Test.Runner
var Assert; //an instance of YAHOO.Test.Assert to save coding
var testSuite; //The YAHOO.Test.Suite that will get run.

/**
 * Sets the required value for the name property on the given template, creates
 * and returns a new YUI Test.Case object.
 * 
 * @param template the template object containing all of the tests
 */
function setupTestCase(template) {
    template.name = "jsTestCase";
    var test_case = new yui_instance.Test.Case(template);
    return test_case;
}

/**
 * Sets up the test suite with a single test case using the given 
 * template.
 * 
 * @param template the template object containing all of the tests
 */
function setupTestSuite(template) {
    var test_case = setupTestCase(template);
    testSuite = new yui_instance.Test.Suite("Bond JS Test Suite");
    testSuite.add(test_case);
}

/**
 * Runs the YAHOO.Test.Suite
 */
function runTestSuite() {
    runner = yui_instance.Test.Runner;
    Assert = yui_instance.Assert;

    runner.clear();
    runner.add(testSuite);
    runner.run();
}

/**
 * Used to see if the YAHOO.Test.Runner is still running.  The
 * test results are not available until it is done running.
 */
function isRunning() {
    return runner.isRunning();
}

/**
 * Gets the results from the YAHOO.Test.Runner
 */
function getTestResults() {
    return runner.getResults(yui_instance.Test.Format.JSON);
}

Per quanto riguarda il selenio, abbiamo utilizzato un test parametrizzato.Eseguiamo i nostri test sia in IE che in FireFox nel metodo dati, analizzando i risultati del test in un elenco di array di oggetti con ciascun array contenente il nome del browser, il nome del file di test, il nome del test, il risultato (passato, fallito o ignorato) e il messaggio.

Il test vero e proprio asserisce semplicemente il risultato del test.Se non è uguale a "pass" allora fallisce il test con il messaggio restituito dal risultato del test YUI.

    @Parameters
public static List<Object[]> data() throws Exception {
    yui_test_codebase = "file:///c://myapppath/yui/tests";

    List<Object[]> testResults = new ArrayList<Object[]>();

    pageNames = new ArrayList<String>();
    pageNames.add("yuiTest1.html");
    pageNames.add("yuiTest2.html");

    testResults.addAll(runJSTestsInBrowser(IE_NOPROXY));
    testResults.addAll(runJSTestsInBrowser(FIREFOX));
    return testResults;
}

/**
 * Creates a selenium instance for the given browser, and runs each
 * YUI Test page.
 * 
 * @param aBrowser
 * @return
 */
private static List<Object[]> runJSTestsInBrowser(Browser aBrowser) {
    String yui_test_codebase = "file:///c://myapppath/yui/tests/";
    String browser_bot = "this.browserbot.getCurrentWindow()"
    List<Object[]> testResults = new ArrayList<Object[]>();
    selenium = new DefaultSelenium(APPLICATION_SERVER, REMOTE_CONTROL_PORT, aBrowser.getCommand(), yui_test_codebase);
    try {
        selenium.start();

        /*
         * Run the test here
         */
        for (String page_name : pageNames) {
            selenium.open(yui_test_codebase + page_name);
            //Wait for the YAHOO instance to be available
            selenium.waitForCondition(browser_bot + ".yui_instance != undefined", "10000");
            selenium.getEval("dom=runYUITestSuite(" + browser_bot + ")");

            //Output from the tests is not available until 
            //the YAHOO.Test.Runner is done running the suite
            selenium.waitForCondition("!" + browser_bot + ".isRunning()", "10000");
            String output = selenium.getEval("dom=getYUITestResults(" + browser_bot + ")");

            JSONObject results = JSONObject.fromObject(output);
            JSONObject test_case = results.getJSONObject("jsTestCase");
            JSONArray testCasePropertyNames = test_case.names();
            Iterator itr = testCasePropertyNames.iterator();

            /*
             * From the output, build an array with the following:
             *  Test file
             *  Test name
             *  status (result)
             *  message
             */
            while(itr.hasNext()) {
                String name = (String)itr.next();
                if(name.startsWith("test")) {
                    JSONObject testResult = test_case.getJSONObject(name);
                    String test_name = testResult.getString("name");
                    String test_result = testResult.getString("result");
                    String test_message = testResult.getString("message");
                    Object[] testResultObject = {aBrowser.getCommand(), page_name, test_name, test_result, test_message};
                    testResults.add(testResultObject);
                }
            }

        }
    } finally {
        //if an exception is thrown, this will guarantee that the selenium instance
        //is shut down properly
        selenium.stop();
        selenium = null;
    }
    return testResults;
}
/**
 * Inspects each test result and fails if the testResult was not "pass"
 */
@Test
public void inspectTestResults() {
    if(!this.testResult.equalsIgnoreCase("pass")) {
        fail(String.format(MESSAGE_FORMAT, this.browser, this.pageName, this.testName, this.message));
    }
}

Spero che questo sia utile.

C'è un nuovo progetto che ti permette di correre qunità test in un ambiente Java (come Ant) in modo da poter integrare completamente la tua suite di test lato client con gli altri test unitari.

http://qunit-test-runner.googlecode.com

L'ho usato per testare i plugin jQuery, objx codice, JavaScript OO personalizzato e funziona per tutto senza modifiche.

Il progetto a cui sto lavorando utilizza Js-Test-Driver ospitando Gelsomino su Chrome 10 con Adattatore Jasmine-JSTD compreso l'utilizzo Copertura del codice test inclusi in JS-Test-Driver.Sebbene si verifichino alcuni problemi ogni volta che cambiamo o aggiorniamo i browser su Ambiente CI i test di Jasmine funzionano abbastanza bene con solo problemi minori con i test asincroni, ma per quanto ne so questi possono essere risolti utilizzando Jasmine Clock ma non ho ancora avuto la possibilità di risolverli.

Ho pubblicato un piccola biblioteca per verificare i test JavaScript dipendenti dal browser senza dover utilizzare un browser.È un modulo node.js che utilizza zombie.js per caricare la pagina di test e ispezionare i risultati.Ne ho scritto sul mio blog.Ecco come appare l'automazione:

var browsertest = require('../browsertest.js').browsertest;

describe('browser tests', function () {

it('should properly report the result of a mocha test page', function (done) {
    browsertest({
        url: "file:///home/liam/work/browser-js-testing/tests.html",
        callback: function() { 
            done();
        }
    });
});

});

Ho esaminato la data della tua domanda e allora c'erano alcune buone librerie/framework di test JS.Oggi puoi trovare molto di più e con focus diversi come TDD, BDD, Assetion e con/senza supporto per i corridori.

Ci sono molti giocatori in questo gioco come Mocha, Chai, QUnit, Jasmine, ecc...Puoi trovare qualche informazione in più in Questo blog sui test JS/Mobile/web...

Un altro framework di test JS che può essere eseguito con Ant è Controllo incrociato.C'è un esempio di esecuzione di CrossCheck tramite Ant nel file di build per il progetto.

CrossCheck tenta, con successo limitato, di emulare un browser, comprese implementazioni in stile mock di XMLHttpRequest e timeout/intervallo.

Tuttavia, al momento non gestisce il caricamento di JavaScript da una pagina Web.Devi specificare i file javascript che desideri caricare e testare.Se mantieni tutto il tuo JS separato dal tuo HTML, potrebbe funzionare per te.

Ho scritto un'attività Ant che utilizza Fantasma JS, un browser webkit headless, per eseguire file di test html QUnit all'interno di un processo di compilazione Ant.Può anche fallire la compilazione se qualche test fallisce.

https://github.com/philmander/ant-jstestrunner

Questa è una buona valutazione di diversi strumenti di test.

Strumenti di test unitari JavaScript per TDD

Personalmente preferiscohttps://code.google.com/p/js-test-driver/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top