Java. Unit and Integration Testing

Содержание

Слайд 2

Agenda

What is Testing
Testing Types
Unit and Integration Testing
JUnit
Emma Coverage
TestNG
Parallel Tests Running

Agenda What is Testing Testing Types Unit and Integration Testing JUnit Emma

Case studies

Слайд 3

What is Testing

Software testing is the process of program execution in order

What is Testing Software testing is the process of program execution in
to find bugs.
Software testing is the process used to measure the quality of developed computer software. Usually, quality is constrained to such topics as:
correctness, completeness, security;
but can also include more technical requirements such as:
capability, reliability, efficiency, portability, maintainability, compatibility, usability, etc.

Слайд 4

Testing Types. Unit Testing

Unit testing is a procedure used to validate that

Testing Types. Unit Testing Unit testing is a procedure used to validate
individual units of source code are working properly.
The goal of unit testing is to isolate each part of the program and show that the individual parts are correct.

Слайд 5

Testing Types. Unit Testing

Unit testing – the process of programming, allowing you

Testing Types. Unit Testing Unit testing – the process of programming, allowing
to test the correctness of the individual modules of the program source.
The idea – to write tests for each non-trivial function or method.
This allows you to:
Check whether an another code change to errors in the field of tested already the program;
Easy the detection and elimination of such errors.
The purpose of unit testing – to isolate certain parts of the program and show that individually these pieces are functional.

Слайд 6

Testing Types. Unit Testing

Task: Implement functionality to calculate speed=distance/time where distance and

Testing Types. Unit Testing Task: Implement functionality to calculate speed=distance/time where distance
time values will be manually entered.
Unit Testing Procedure: Test the implemented functionality with the different distance and time values.
Defect: Crash when entered value time=0.

Слайд 7

Integration Testing

Integration testing is the phase of software testing in which individual

Integration Testing Integration testing is the phase of software testing in which
software modules are combined and tested as a group.
This is testing two or more modules or functions together with the intent of finding interface defects between the modules or functions.

Слайд 8

Integration Testing

Task: Database scripts, application main code and GUI components were developed

Integration Testing Task: Database scripts, application main code and GUI components were
by different programmers. There is need to test the possibility to use these 3 parts as one system.
Integration Testing Procedure: Combine 3 parts into one system and verify interfaces (check interaction with database and GUI).
Defect: “Materials” button action returns not list of books but list of available courses.

Слайд 9

Unit / Integration Testing

public class One {
private String text;
public One(

Unit / Integration Testing public class One { private String text; public
) {
// Code
}
// Functionality
public int calc( ) {
int result = 0;
// Code
return result;
}
// get, set, etc.
}

Слайд 10

Unit / Integration Testing

public class Two {
private One one;
public Two(One

Unit / Integration Testing public class Two { private One one; public
one) {
this.one = one;
}
public String resume( ) {
// Functionality
return one.getText( ) + "_" + one.calc( ).toString( );
}
// Code
}

Слайд 11

Unit / Integration Testing

public class Appl {
public static void main(String[ ]

Unit / Integration Testing public class Appl { public static void main(String[
args) {
One one = new One( );
one.setText("data“);
Two two = new Two(one);
two.resume( );
// etc
}
}

Слайд 12

Unit / Integration Testing

public class TwoTest {
@Test
public void TestResume( )

Unit / Integration Testing public class TwoTest { @Test public void TestResume(
{
One one = new One( );
one.setText("Integration Test“);
Two two = new Two(one);
// TODO Initialize to an appropriate value
String expected = "Integration Test_..."; // Result ???
String actual;
actual = two.resume( );
Assert.AreEqual(expected, actual);
} }

Слайд 13

Unit / Integration Testing

A method stub or simply stub in software development

Unit / Integration Testing A method stub or simply stub in software
is a piece of code used to stand in for some other programming functionality.
A stub may simulate the behavior of existing code or be a temporary substitute for yet-to-be-developed code.
public interface IOne {
void setText(String text);
String getText( );
int calc( );
}

Слайд 14

Unit / Integration Testing

public class One implements IOne {
private String text;

Unit / Integration Testing public class One implements IOne { private String
public One( ) {
// Code
}
// Functionality
public int calc( ) {
int result = 0;
// Code
return result;
}
// get, set, etc.
}

Слайд 15

Unit / Integration Testing

public class Two {
private IOne one;
public Two(IOne

Unit / Integration Testing public class Two { private IOne one; public
one) {
this.one = one;
}
public String resume( ) {
// Functionality
return one.getText( ) + "_" + one.calc( ).toString( );
}
// Code
}

Слайд 16

Unit / Integration Testing

Stub – a piece of code used to stand

Unit / Integration Testing Stub – a piece of code used to
in for some other programming functionality.
public class StubOne extends IOne {
private String text;
public StubOne( ) {
}
public void setText(String text) {
}
public String getText( ) {
return “”
}
public int Calc( ) {
return 0;
}
}

Слайд 17

Unit / Integration Testing

public class TwoTest {
@Test
public void TestResume( )

Unit / Integration Testing public class TwoTest { @Test public void TestResume(
{
IOne one = new StubOne( );
one.setText(" Unit Test“);
Two two = new Two(one);
// TODO Initialize to an appropriate value
String expected = " Unit Test_..."; // Result !!!
String actual;
actual = two.resume( );
Assert.AreEqual(expected, actual);
} }

Слайд 18

Junit 3

import junit.framework.TestCase;
рublic class AddJavaTest extends TestCase {
protected void setUp( )

Junit 3 import junit.framework.TestCase; рublic class AddJavaTest extends TestCase { protected void
throws Exception {
// create object }
protected void tearDown( ) throws Exception {
// to free resources }
public AddJavaTest(String name){
super (name); }
public void testSimpleAddition( ){
assertTrue(expect == actual);
} }

deprecated

Слайд 19

JUnit

Assert class contains many different overloaded methods.
http://www.junit.org/ https://github.com/kentbeck/junit/wiki
assertEquals(long expected, long actual)

JUnit Assert class contains many different overloaded methods. http://www.junit.org/ https://github.com/kentbeck/junit/wiki assertEquals(long expected,
Asserts that two longs are equal.
assertTrue(boolean condition)
Asserts that a condition is true.
assertFalse(boolean condition)
Asserts that a condition is false.
assertNotNull(java.lang.Object object)
Asserts that an object isn't null.
assertNull(java.lang.Object object)
Asserts that an object is null.

Слайд 20

JUnit

assertSame(java.lang.Object expected,
java.lang.Object actual)
Asserts that two objects refer to the same

JUnit assertSame(java.lang.Object expected, java.lang.Object actual) Asserts that two objects refer to the
object.
assertNotSame(java.lang.Object unexpected,
java.lang.Object actual)
Asserts that two objects do not refer to the same object.
assertArrayEquals(byte[] expecteds,
byte[] actuals)
Asserts that two byte arrays are equal.
fail()
Fails a test with no message.
fail(java.lang.String message)
Fails a test with the given message.

Слайд 21

Junit 4

import org.junit.Test;
import org.junit.Assert;
public class MathTest {
@Test
public void testEquals( )

Junit 4 import org.junit.Test; import org.junit.Assert; public class MathTest { @Test public
{
Assert.assertEquals(4, 2 + 2);
Assert.assertTrue(4 == 2 + 2);
}
@Test
public void testNotEquals( ) {
Assert.assertFalse(5 == 2 + 2);
} }

Слайд 22

JUnit

To integrate tests, you can use a class TestSuite.
public static void main(String[

JUnit To integrate tests, you can use a class TestSuite. public static
] args) {
TestRunner runner = new TestRunner( );
TestSuite suite = new TestSuite( );
suite.addTest(new TestClass(“testEquals”));
suite.addTest(new TestClass(“testNotEquals”));
runner.doRun(suite); }
public static Test suite( ) { // without main( ) and runner.
TestSuite suite = new TestSuite( );
suite.addTest(new TestClass(“testEquals”));
suite.addTest(new TestClass(“testNotEquals”));
return suite; }

Слайд 23

JUnit

package com.softserve.edu;
public class Rectangle implements IRectangle {
private double height;
private double

JUnit package com.softserve.edu; public class Rectangle implements IRectangle { private double height;
width;
public Rectangle(double height, double width) {
this.height = height;
this.width = width;
}
public double getHeight( ) {
return height;
}

Слайд 24

JUnit

public void setHeight(double height) {
this.height = height;
}
public double

JUnit public void setHeight(double height) { this.height = height; } public double
getWidth( ) {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double Perimeter( ) {
return 2 * (height + width);
} }

Слайд 25

JUnit

package com.softserve.edu;
public class Square {
private IRectangle rectangle;
public Square(double width) {

JUnit package com.softserve.edu; public class Square { private IRectangle rectangle; public Square(double
rectangle = new Rectangle(width, width);
}
// For testing
public void setSquare(IRectangle rectangle) {
this.rectangle = rectangle;
}

Слайд 26

JUnit

public double Perimeter(int multiply) {
double p = 1;
for (int

JUnit public double Perimeter(int multiply) { double p = 1; for (int
i = 0; i < multiply; i++) {
p = p * rectangle.Perimeter( );
}
return p;
} }
package com.softserve.edu;
public interface IRectangle {
public double Perimeter( );
}

Слайд 31

JUnit

package com.softserve.edu;
import org.junit.Assert;
import org.junit.Test;
public class SquareTest {
@Test
public void testPerimeter( )

JUnit package com.softserve.edu; import org.junit.Assert; import org.junit.Test; public class SquareTest { @Test
{
Square square = new Square(2);
double expected = 64; // Result ???
double actual;
actual = square.Perimeter(2); // Method with error
Assert.assertEquals(expected, actual);
//fail("Not yet implemented"); }

Слайд 32

JUnit

@Test
public void testPerimeter2( ) {
Square square = new Square(2);

JUnit @Test public void testPerimeter2( ) { Square square = new Square(2);
square.setSquare(new RectangleStub( ))
double expected = 4;
double actual;
actual = target.Perimeter(2);
Assert.assertEquals(expected, actual);
//fail("Not yet implemented");
}
}

Слайд 33

JUnit

Stub – a piece of code used to stand in for some

JUnit Stub – a piece of code used to stand in for
other programming functionality.
package com.softserve.edu;
public class RectangleStub implements IRectangle {
public double Perimeter( ) {
return 2;
}
}

Слайд 34

Emma Coverage

EclEmma – Java Code Coverage for Eclipse.

Emma Coverage EclEmma – Java Code Coverage for Eclipse.

Слайд 35

Emma Coverage

Emma Coverage

Слайд 36

TestNG

JUnit 4 and TestNG are both very popular unit test framework in

TestNG JUnit 4 and TestNG are both very popular unit test framework
Java.
TestNG (Next Generation) is a testing framework which inspired by JUnit and NUnit, but introducing many new innovative functionality like dependency testing, grouping concept to make testing more powerful and easier to do. It is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc.

Слайд 37

TestNG

Setting up TestNG with Eclipse.
Click Help –> Install New Software
Type http://beust.com/eclipse in

TestNG Setting up TestNG with Eclipse. Click Help –> Install New Software
the "Work with" edit box and click ‘Add’ button.
Click Next and click on the radio button "I accept the terms of the license agreement“.
After the installation, it will ask for restart of Eclipse. Then restart the Eclipse.
Once the Eclipse is restarted, we can see the TestNG icons & menu items as in the below figures.

Слайд 38

TestNG

To create a new TestNG class, select the menu File/New/TestNG

TestNG To create a new TestNG class, select the menu File/New/TestNG

Слайд 39

TestNG

The first page of the wizard will show you a list of

TestNG The first page of the wizard will show you a list
all the public methods of that class and it will give you the option to select the ones you want to test. Each method you select on this page will be included in the new TestNG class with a default implementation.

Слайд 40

TestNG

The next page lets you specify where that file will be created

TestNG The next page lets you specify where that file will be created

Слайд 41

TestNG

package com.softserve.edu;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;
public class RectangleTest {
@Test
public void Perimeter( )

TestNG package com.softserve.edu; import org.testng.AssertJUnit; import org.testng.annotations.Test; public class RectangleTest { @Test
{
Rectangle rectangle = new Rectangle(2, 3);
double expected = 10;
double actual;
actual = rectangle.Perimeter( );
System.out.println(actual);
AssertJUnit.assertEquals(expected, actual);
// throw new RuntimeException("Test not implemented");
} }

Слайд 42

TestNG

Hierarchy of tests
All tests belong to a sequence of tests (suite), include

TestNG Hierarchy of tests All tests belong to a sequence of tests
a number of classes, each consisting of several test methods. Classes and test methods can belong to a group.
+- suite/
+- test0/
| +- class0/
| | +- method0(integration group)/
| | +- method1(functional group)/
| | +- method2/
| +- class1
| +- method3(optional group)/
+- test1/
+- class3(optional group, integration group)/
+- method4/

Слайд 43

TestNG

Every available before and after configurators. Started it all in the order
+-

TestNG Every available before and after configurators. Started it all in the
before suite/
+- before group/
+- before test/
+- before class/
+- before method/
+- test/
+- after method/
...
+- after class/
...
+- after test/
...
+- after group/
...
+- after suite/

Слайд 44

TestNG

You can create a TestNG Launch Configuration. Select the Run / Debug

TestNG You can create a TestNG Launch Configuration. Select the Run /
menu and create a new TestNG configuration.

Слайд 45

TestNG

You should change the name of this configuration.
Then you choose to launch

TestNG You should change the name of this configuration. Then you choose
your TestNG tests in the following ways
From a class file
Make sure the box near Class is checked and then pick a class from your project.
This list only contains classes that contain TestNG annotations
From groups
If you only want to launch one or several groups, you can type them in the text field or pick them from a list

Слайд 46

TestNG

From a definition file
You can select a suite definition from your project.
It

TestNG From a definition file You can select a suite definition from
doesn't have to be named testng.xml, the plug-in will automatically identify all the applicable TestNG XML files in your project
From a method
This launch isn't accomplished from the Launch dialog but directly from your Outline view

Слайд 47

TestNG

You start tests in many different ways: from an XML file, from

TestNG You start tests in many different ways: from an XML file,
a method, a class, etc.
You can configure the settings for all the launches that are not done from an XML file.
In order to give you access to the most flexibility, TestNG lets you specify an XML suite file for all these launches, which you can find in the Preferences menu.

Слайд 48

TestNG

The Summary tab gives you statistics on your test run, such as

TestNG The Summary tab gives you statistics on your test run, such
the timings, the test names, the number of methods and classes, etc.
You can easily convert JUnit 3 and JUnit 4 tests to TestNG.

Слайд 49

TestNG

Basic usage
import java.util.*;
import org.testng.Assert;
import org.testng.annotations.*;
public class TestNGTest1 {
private Collection collection;
@BeforeClass

TestNG Basic usage import java.util.*; import org.testng.Assert; import org.testng.annotations.*; public class TestNGTest1
public void oneTimeSetUp( ) {
System.out.println("@BeforeClass - oneTimeSetUp"); }
@AfterClass
public void oneTimeTearDown( ) {
// one-time cleanup code
System.out.println("@AfterClass - oneTimeTearDown"); }

Слайд 50

TestNG

@BeforeMethod
public void setUp( ) {
collection = new ArrayList( );

TestNG @BeforeMethod public void setUp( ) { collection = new ArrayList( );
System.out.println("@BeforeMethod - setUp");
}
@AfterMethod
public void tearDown( ) {
collection.clear( );
System.out.println("@AfterMethod - tearDown");
}

Слайд 51

TestNG

@Test
public void testEmptyCollection( ) {
Assert.assertEquals(collection.isEmpty( ),true);
System.out.println("@Test - testEmptyCollection");

TestNG @Test public void testEmptyCollection( ) { Assert.assertEquals(collection.isEmpty( ),true); System.out.println("@Test - testEmptyCollection");
}
@Test
public void testOneItemCollection( ) {
collection.add("itemA");
Assert.assertEquals(collection.size( ),1);
System.out.println("@Test - testOneItemCollection");
}
}

Слайд 52

TestNG

Result
@BeforeClass - oneTimeSetUp
@BeforeMethod - setUp
@Test - testEmptyCollection
@AfterMethod - tearDown
@BeforeMethod - setUp
@Test -

TestNG Result @BeforeClass - oneTimeSetUp @BeforeMethod - setUp @Test - testEmptyCollection @AfterMethod
testOneItemCollection
@AfterMethod - tearDown
@AfterClass - oneTimeTearDown

Слайд 53

TestNG

Expected Exception Test
The divisionWithException() method will throw an ArithmeticException Exception, since

TestNG Expected Exception Test The divisionWithException() method will throw an ArithmeticException Exception,
this is an expected exception, so the unit test will pass.
import org.testng.annotations.*;
public class TestNGTest2 {
@Test(expectedExceptions = ArithmeticException.class)
public void divisionWithException( ) {
int i = 1/0;
}
}

Слайд 54

TestNG

Ignore Test
TestNG will not test the divisionWithException() method
import org.testng.annotations.*;
public class TestNGTest3

TestNG Ignore Test TestNG will not test the divisionWithException() method import org.testng.annotations.*;
{
@Test(enabled=false)
public void divisionWithException( ) {
System.out.println("Method is not ready yet");
}
}

Слайд 55

TestNG

Time Test
The “Time Test” means if an unit test takes longer

TestNG Time Test The “Time Test” means if an unit test takes
than the specified number of milliseconds to run, the test will terminated and mark as failed
import org.testng.annotations.*;
public class TestNGTest4 {
@Test(timeOut = 1000)
public void infinity( ) {
while (true);
}
}

Слайд 56

TestNG

Suite Test. The “Suite Test” means bundle a few unit test cases

TestNG Suite Test. The “Suite Test” means bundle a few unit test
and run it together.
In TestNG, XML file is use to define the suite test.
"http://beust.com/testng/testng-1.0.dtd" >








"TestNGTest1" and "TestNGTest2" will execute together

Слайд 57

TestNG

TestNG provides a “Grouping” feature to bundle few methods as a single

TestNG TestNG provides a “Grouping” feature to bundle few methods as a
unit for testing
import org.testng.annotations.*;
public class TestNGTest5 {
@Test(groups="method1")
public void testingMethod1( ) {
System.out.println("Method - testingMethod1( )");
}
@Test(groups="method2")
public void testingMethod2( ) {
System.out.println("Method - testingMethod2( )");
}

Слайд 58

TestNG

@Test(groups="method1")
public void testingMethod1_1( ) {
System.out.println("Method - testingMethod1_1( )");
}

TestNG @Test(groups="method1") public void testingMethod1_1( ) { System.out.println("Method - testingMethod1_1( )"); }
@Test(groups="method4")
public void testingMethod4( ) {
System.out.println("Method - testingMethod4( )");
}
}
// Every method is tie to a group.

Слайд 59

TestNG

You can execute the unit test with group “method1” only

TestNG You can execute the unit test with group “method1” only "http://beust.com/testng/testng-1.0.dtd"
SYSTEM
"http://beust.com/testng/testng-1.0.dtd" >












Result
Method - testingMethod1_1( )
Method - testingMethod1( )

Слайд 60

TestNG

Parameterized Test
In TestNG, XML file or “@DataProvider” is used to provide

TestNG Parameterized Test In TestNG, XML file or “@DataProvider” is used to
vary parameter for unit testing.
Declare “@Parameters” annotation in method which needs parameter testing, the parametric data will be provide by TestNG’s XML configuration files. By doing this, you can reuse a single test case with different data sets easily.
import org.testng.annotations.*;
public class TestNGTest6 {
@Test
@Parameters(value="number")
public void parameterIntTest(int number) {
System.out.println("Parameterized Number is: " +
number);
} }

Слайд 61

TestNG

"http://beust.com/testng/testng-1.0.dtd" >


TestNG "http://beust.com/testng/testng-1.0.dtd" > Result: Parameterized Number is : 2
value="2"/>





Result: Parameterized Number is : 2

Слайд 62

TestNG

Test cases occasionally may require complex data types, which can’t be represented

TestNG Test cases occasionally may require complex data types, which can’t be
as a String or a primitive value in XML file. TestNG handles this scenario with @DataProvider annotation, which facilitates the mapping of complex parameter types to a test method.
import org.testng.annotations.*;
public class TestNGTest7 {
@Test(dataProvider = "Data-Provider-Function")
public void parameterIntTest(Class clzz, String[ ] number) {
System.out.println("Parameterized Number is: " +
number[0]);
System.out.println("Parameterized Number is: " +
number[1]);
}

Слайд 63

TestNG

// This function will provide the parameter data
@DataProvider(name = "Data-Provider-Function")

TestNG // This function will provide the parameter data @DataProvider(name = "Data-Provider-Function")
public Object[ ][ ] parameterIntTestProvider( ) {
return new Object[ ][ ]{
{Vector.class, new String[ ] {"java.util.AbstractList",
"java.util.AbstractCollection"}},
{String.class, new String[ ] {"1", "2"}},
{Integer.class, new String[ ] {“3", “4"}}
};
}
}

Parameterized Number is : java.util.AbstractList
Parameterized Number is : java.util.AbstractCollection
Parameterized Number is : 1
Parameterized Number is : 2
Parameterized Number is : 1
Parameterized Number is : 2

Слайд 64

TestNG

public class TestNGTest8 {
private int number;
private String msg;
public void

TestNG public class TestNGTest8 { private int number; private String msg; public
setNumber(int number){
this.number = number; }
public int getNumber( ){
return this.number; }
public void setMsg(String msg){
this.msg = msg; }
public String getMsg( ){
return this.msg; }
}

Слайд 65

TestNG

import org.testng.annotations.*;
public class TestNGTest9 {
@Test(dataProvider = "Data-Provider-Function")
public void parameterIntTest(TestNGTest8 clzz)

TestNG import org.testng.annotations.*; public class TestNGTest9 { @Test(dataProvider = "Data-Provider-Function") public void
{
System.out.println("Number is: " + clzz.getMsg( ));
System.out.println("Number is: " + clzz.getNumber( )); }
@DataProvider(name = "Data-Provider-Function")
public Object[ ][ ] parameterIntTestProvider( ) {
TestNGTest8 obj = new TestNGTest8( );
obj.setMsg("Hello");
obj.setNumber(123);
return new Object[ ][ ]{ { obj } };
} }

Слайд 66

TestNG

Testing example. Converting the character to ASCII or vice verse.
package com.softserve.edu;
public class

TestNG Testing example. Converting the character to ASCII or vice verse. package
CharUtils
{
public static int CharToASCII(final char character){
return (int)character;
}
public static char ASCIIToChar(final int ascii){
return (char)ascii;
}
}

Слайд 67

TestNG

import org.testng.Assert;
import org.testng.annotations.*;
public class CharUtilsTest {
@DataProvider
public Object[ ][ ] ValidDataProvider(

TestNG import org.testng.Assert; import org.testng.annotations.*; public class CharUtilsTest { @DataProvider public Object[
) {
return new Object[ ][ ]{
{ 'A', 65 }, { 'a', 97 },
{ 'B', 66 }, { 'b', 98 },
{ 'C', 67 }, { 'c', 99 },
{ 'D', 68 }, { 'd', 100 },
{ 'Z', 90 }, { 'z', 122 },
{ '1', 49 }, { '9', 57 }, };
}

Слайд 68

TestNG

@Test(dataProvider = "ValidDataProvider")
public void CharToASCIITest(final char character,
final int ascii)

TestNG @Test(dataProvider = "ValidDataProvider") public void CharToASCIITest(final char character, final int ascii)
{
int result = CharUtils.CharToASCII(character);
Assert.assertEquals(result, ascii); }
@Test(dataProvider = "ValidDataProvider")
public void ASCIIToCharTest(final char character,
final int ascii) {
char result = CharUtils.ASCIIToChar(ascii);
Assert.assertEquals(result, character);
} }

Слайд 69

TestNG

Dependency Test. TestNG uses “dependOnMethods“ to implement the dependency testing. If the

TestNG Dependency Test. TestNG uses “dependOnMethods“ to implement the dependency testing. If
dependent method fails, all the subsequent test methods will be skipped, not marked as failed.
The “method2( )” will execute only if “method1( )” is run successfully, else “method2( )” will skip
import org.testng.annotations.*;
public class TestNGTest10 {
@Test
public void method1( ) {
System.out.println("This is method 1"); }
@Test(dependsOnMethods={"method1"})
public void method2( ) {
System.out.println("This is method 2"); } }

Слайд 70

TestNG. Parallel Tests Running

Multithreading. Tests were performed simultaneously on multiple threads.
public class

TestNG. Parallel Tests Running Multithreading. Tests were performed simultaneously on multiple threads.
ConcurrencyTest1 extends Assert {
private Map data;
@BeforeClass
void setUp( ) throws Exception {
data = new HashMap( );
}
@AfterClass
void tearDown( ) throws Exception {
data = null;
}

Слайд 71

TestNG. Parallel Tests Running

@Test(threadPoolSize = 30,
invocationCount = 100,
invocationTimeOut = 10000)

TestNG. Parallel Tests Running @Test(threadPoolSize = 30, invocationCount = 100, invocationTimeOut =
public void testMapOperations( ) throws Exception {
data.put("1", "111"); data.put("2", "111");
data.put("3", "111"); data.put("4", "111");
data.put("5", "111"); data.put("6", "111");
data.put("7", "111");
for (Map.Entry entry : data.entrySet( )) {
System.out.println(entry); }
data.clear( );
}

Слайд 72

TestNG

@Test(singleThreaded = true, // run in a single thread
invocationCount =

TestNG @Test(singleThreaded = true, // run in a single thread invocationCount =
100,
invocationTimeOut = 10000)
public void testMapOperationsSafe( ) throws Exception {
data.put("1", "111"); data.put("2", "111");
data.put("3", "111"); data.put("4", "111");
data.put("5", "111"); data.put("6", "111");
data.put("7", "111");
for (Map.Entry entry : data.entrySet( )) {
System.out.println(entry); }
data.clear( ); }
}

Слайд 73

TestNG

public class ConcurrencyTest2 extends Assert {
// some staff here
@DataProvider(parallel =

TestNG public class ConcurrencyTest2 extends Assert { // some staff here @DataProvider(parallel
true)
public Object[ ][ ] concurrencyData( ) {
return new Object[ ][ ] {
{"1", "2"}, {"3", "4"},
{"5", "6"}, {"7", "8"},
{"9", "10"}, {"11", "12"},
{"13", "14"}, {"15", "16"},
{"17", "18"}, {"19", "20"},
};
}

Слайд 74

TestNG

Set parallel option at the date of the provider in true.
Tests for

TestNG Set parallel option at the date of the provider in true.
each data set to be launched in a separate thread.
@Test(dataProvider = "concurrencyData")
public void testParallelData(String first, String second) {
final Thread thread = Thread.currentThread( );
System.out.printf("#%d %s: %s : %s", thread.getId( ),
thread.getName( ), first, second);
System.out.println( );
}
}
Имя файла: Java.-Unit-and-Integration-Testing.pptx
Количество просмотров: 41
Количество скачиваний: 0