dennisegen / MOST

MOST - Metabolic Optimization and Simulation Tool
3 stars 4 forks source link

JUnit Testing #98

Open mentatpsi opened 10 years ago

mentatpsi commented 10 years ago

Hi guys, I'm currently working on incorporating JUnit, which is a unit testing framework design for automating testing the validity of operations, into MOST. I was wondering if there were any particular tests that you guys think would be best?

I'm going to list a couple that I've been thinking of, just to give an idea of what can be done:

Basic:

Intermediate:

Eventually, it could be possible to have a "developer" MOST application that has an additional toolbar menu for testing, but this would be a much later goal.

mentatpsi commented 10 years ago

Sorry about the delay guys. I've provided a basic proof of concept below.

The main component is,

assertTrue(this.reader(file1,file2), FileUtils.contentEquals(file1, file2));

which upon determining that the two files differ, calls upon the reader method. The reader method just detects the differences and outputs into the trace. It can be run with Eclipse using Alt+Shift+X T. Tests can easily be created by adding the @Test annotation above a method. The assertTrue method takes as arguments a String and boolean, so decided a little trick is to create a method that returns a string to accommodate.

This isn't as practical as might be desired as the existence of a single line that's different in the second file will register the rest different. A more ideal solution would be to contain the strings in an ArrayList and detect afterwards what differences file2 has. I'll try to work on that a bit more throughout the week.

When I can get a bit more regular access to download the fork I'll work to incorporate it further. Apologies about the aesthetics of the code.

Code:

import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import junit.framework.AssertionFailedError;

import org.junit.Test;
import org.apache.commons.io.FileUtils;

public class Tested {
    @Test
    public void main() {
        // TODO Auto-generated method stub
        File file1 = new File("C:\\tests\\Ec_core_flux1.xml");
        //File file2 = file1;
        File file2 = new File("C:\\tests\\Ec_core_flux1_no_boundaries.xml");
        try {
            assertTrue(this.reader(file1,file2), FileUtils.contentEquals(file1, file2));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (AssertionFailedError e){
            try {
                this.reader(file1, file2);
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }

    public String reader(File f1, File f2) throws FileNotFoundException, IOException{

        FileReader fR1 = new FileReader(f1);
        FileReader fR2 = new FileReader(f2);

        BufferedReader reader1 = new BufferedReader(fR1);
        BufferedReader reader2 = new BufferedReader(fR2);

        String line1 = null;
        String line2 = null;
        String cur = "Files Differ\n";
        cur += "Differences:\n";
        int count = 0;
        while (((line1 = reader1.readLine()) != null)
                && ((line2 = reader2.readLine()) != null)) {
            if (!line1.equalsIgnoreCase(line2)) {
                cur += "line " + count + ": " + line2 +"\n";
            }
            count += 1;
        }
        reader1.close();
        reader2.close();
        //System.out.println("Flag " + flag);
        return cur;
    }

}

Output:

java.lang.AssertionError: Files Differ
Differences:
line 80: </listOfSpecies>
line 81: <listOfReactions>
line 82:    <reaction id="R_ACKr" name="R_acetate_kinase" reversible="true">
line 83:        <notes>
line 84:            <html:p>GENE_ASSOCIATION: b2296</html:p>
line 85:            <html:p>PROTEIN_ASSOCIATION: AckA</html:p>
line 86:            <html:p>SUBSYSTEM: S_Pyruvate_Metabolism</html:p>
line 87:            <html:p>PROTEIN_CLASS: 2.7.2.1</html:p>
line 88:        </notes>
line 89:        <listOfReactants>
line 90:            <speciesReference species="M_ac_c" stoichiometry="1.000000"/>
line 91:            <speciesReference species="M_atp_c" stoichiometry="1.000000"/>
line 92:        </listOfReactants>
line 93:        <listOfProducts>
line 94:            <speciesReference species="M_actp_c" stoichiometry="1.000000"/>
line 95:            <speciesReference species="M_adp_c" stoichiometry="1.000000"/>
line 96:        </listOfProducts>
line 97:        <kineticLaw>
line 98:            <math xmlns="http://www.w3.org/1998/Math/MathML">
line 99:                <ci> FLUX_VALUE </ci>
line 100:           </math>
line 101:           <listOfParameters>

...
line 2224: </listOfReactions>
line 2225: </model>
line 2226: </sbml>

    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.assertTrue(Assert.java:41)
    at Tested.main(Tested.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

In order to get JUnit and this test working, the following external jars have to be added:

hamcrest-core-1.3.jar junit-4.11.jar commons-io-2.4.jar bin

mentatpsi commented 10 years ago

In the future, this code is on JUnit's website:

@Test
    public void lookupEmailAddresses() {
        assertThat(new CartoonCharacterEmailLookupService().getResults("looney"), allOf(
            not(empty()), 
            containsInAnyOrder(
                allOf(instanceOf(Map.class), hasEntry("id", "56"), hasEntry("email", "roadrunner@fast.org")),
                allOf(instanceOf(Map.class), hasEntry("id", "76"), hasEntry("email", "wiley@acme.com"))
            )
        ));
    }*/

It can be used for easy mapping tests.