|
|
| Línea 1: |
Línea 1: |
| − | [[TwiP]] ("Tests with Parameters") permite agregar parámetros a los métodos de prueba [[JUnit]] de forma simple. [[TwiP]] llama a los métodos con todas las combinaciones posibles de sus parámetros... o por lo menos un subconjunto razonable de los valores comunes en el caso de los Integers, Strings, etc.
| + | Ya learn something new eveyardy. It's true I guess! |
| − | | |
| − | Se puede definir un conjunto de valores propios para su uso y/o se puede reducir los valores fijados con la anotación "Assume", por ejemplo, @Assume("> = 0"). Los valores por defecto existen para los tipos primitivos (int, etc), sus wrappers de clase (Integer, etc), enum y Strings. Para otros tipos o para un conjunto de valores diferentes, se puede definir un método estático o un atributo y anotar los parámetros de su método de prueba, o puede anotar en el campo o método para inyectar los valores por tipo.
| |
| − | | |
| − | == Requerimientos ==
| |
| − | | |
| − | [[TwiP]] requiere [[JUnit]] 4.5 and [[Java]] 5.
| |
| − | | |
| − | == Test de tipos primitivos ==
| |
| − | | |
| − | <code java5>
| |
| − | import net.sf.twip.Assume;
| |
| − | import net.sf.twip.TwiP;
| |
| − | import org.junit.Test;
| |
| − | import org.junit.runner.RunWith;
| |
| − | | |
| − | @RunWith(TwiP.class)
| |
| − | public class tiposTest {
| |
| − | | |
| − | @Test
| |
| − | public void intTest(int numero) {
| |
| − | System.out.println(numero);
| |
| − | }
| |
| − | | |
| − | @Test
| |
| − | public void integerTest(Integer numero) {
| |
| − | System.out.println(numero);
| |
| − | }
| |
| − | | |
| − | @Test
| |
| − | public void doubleTest(Double numero) {
| |
| − | System.out.println(numero);
| |
| − | }
| |
| − | | |
| − | @Test
| |
| − | public void stringTest(@Assume("=null") String string) {
| |
| − | System.out.println("| " + string + " |");
| |
| − | }
| |
| − | | |
| − | @Test
| |
| − | public void booleanTest(boolean flag1) {
| |
| − | System.out.println(flag1);
| |
| − | }
| |
| − | }
| |
| − | </code>
| |
| − | | |
| − | == Test con un objeto propio ==
| |
| − | ==== El objeto Persona ====
| |
| − | <code java5>
| |
| − | public class Persona {
| |
| − | | |
| − | private int id;
| |
| − | | |
| − | private String nombre;
| |
| − | | |
| − | public String getApellido() {
| |
| − | return apellido;
| |
| − | }
| |
| − | | |
| − | public void setApellido(String apellido) {
| |
| − | this.apellido = apellido;
| |
| − | }
| |
| − | | |
| − | String apellido;
| |
| − | | |
| − | public Persona(int id, String nombre, String apellido) {
| |
| − | this.id = id;
| |
| − | this.nombre = nombre;
| |
| − | this.apellido = apellido;
| |
| − | }
| |
| − | | |
| − | public Persona(int id, String nombre) {
| |
| − | this.id = id;
| |
| − | this.nombre = nombre;
| |
| − | }
| |
| − | | |
| − | | |
| − | public int getId() {
| |
| − | return id;
| |
| − | }
| |
| − | | |
| − | public void setId(int id) {
| |
| − | this.id = id;
| |
| − | }
| |
| − | | |
| − | public String getNombre() {
| |
| − | return nombre;
| |
| − | }
| |
| − | | |
| − | public void setNombre(String nombre) {
| |
| − | this.nombre = nombre;
| |
| − | }
| |
| − | | |
| − | public String toString() {
| |
| − | return id+" - "+nombre;
| |
| − | }
| |
| − | }
| |
| − | </code>
| |
| − | | |
| − | ==== La prueba JUnit de Persona ====
| |
| − | <code java5>
| |
| − | import net.sf.twip.Assume;
| |
| − | import net.sf.twip.AutoTwip;
| |
| − | import net.sf.twip.TwiP;
| |
| − | import net.sf.twip.Values;
| |
| − | import org.junit.Test;
| |
| − | import org.junit.runner.RunWith;
| |
| − | | |
| − | @RunWith(TwiP.class)
| |
| − | public class PersonaTest {
| |
| − | | |
| − | public static final Persona[] PERSONAS = {new Persona(1, "nombre1"), new Persona(2, "nombre2")};
| |
| − | | |
| − | @AutoTwip
| |
| − | public static Persona crearPersona(int id, String nombre, String apellido) {
| |
| − | return new Persona(id, nombre, apellido);
| |
| − | }
| |
| − | | |
| − | @Test
| |
| − | public void personasTest(@Values("PERSONAS") Persona persona) {
| |
| − | System.out.println(persona);
| |
| − | }
| |
| − | | |
| − | @Test
| |
| − | public void personaTest(@Assume("=null") Persona persona) {
| |
| − | System.out.println(persona);
| |
| − | }
| |
| − | }
| |
| − | </code>
| |
| − | | |
| − | == Ver también ==
| |
| − | *[http://twip.sourceforge.net/ TwiP - Tests with Parameters]
| |
| − | | |
| − | [[Category:JUnit]]
| |
Ya learn something new eveyardy. It's true I guess!