Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [recommenders-dev] How to start with Recommender's source code

Hi Cheng,

thanks for the detailed examples. Test case generation is indeed an interesting and useful topic for software developers. But I'm not sure how much can be achieved in three months. In addition there are tools out there that already do test case generation as Code Analytix Pro for instance (http://code.google.com/javadevtools/codepro/doc/features/junit/test_case_generation.html). There is also a lot of research work on test generation I'm not very familiar with (but know about their complexity at least).

So, I wonder whether your proposal wouldn't  be better positioned as extensions to existing tools that already have test case generation support?

Despite that: are you interested in doing your GSOC in the scope of Eclipse.org even if it's not exactly this topic? I can imagine that you have a bunch of other ideas on how to improve a developer's day-to-day live, right? ;)

Best,
Marcel



On 16.02.2012, at 15:47, chen cheng wrote:

Hi Marcel,

Class object simulate is just a small part of my idea, we can use mocking frameworks such as mockito to help us do some object simulate job, but that is not all. This tool is committed to create all necessary unit test code for a special method or a class with multiple methods. It helps saving our time, create unit test code for us intelligently.

For example, we have such a simple method which was used to math operations:

public static double operate(String ope, double numOne, double numTwo){
if(ope.equals("sum"))
return numOne + numTwo;
else if(ope.equals("min"))
return numOne - numTwo;
else if(ope.equals("mul"))
return numOne * numTwo;
else if(ope.equals("div")){
if(numTwo != 0)
return numOne / numTwo;
}
return Double.MIN_VALUE;
}

Now, we use this tool to create unit test code for this method, it will work like this:
This method has three parameters: ope, numOne and numTwo.

"ope" was compared four times in the method:
ope.equals("sum"), ope.equals("min"), (ope.equals("mul") and ope.equals("div").
So "ope" in test cases will cover at least this four values "sum", "min", "mul" and "div", and some other generated  strings by *String Generate Alogirthm* (NOTICE: user can specify regular _expression_ to generate string values).

"numTwo" was compared one time in the method:
numTwo != 0
So "numTwo" in test cases will cover at least 0 and some other generated numbers.

And we can not find "numOne" detail compare statement, so we just generate by a *Integer Generate Algorithm*

About return value, user must special the *CORRECT* value for each test case manually. Now we get at least these five test cases (user can specify test cases' number, so we should have more, i just list five here to explain how this tool works):
@Test
public void test1() {
String operate = "sum";
assertTrue(hello.operate(operate, 24, 5) == input_ret);
}
@Test
public void test2() {
String operate = "min";
assertTrue(hello.operate(operate, 23, 0) ==  input_ret );
}
@Test
public void test3() {
String operate = "mul";
assertTrue(hello.operate(operate, 2, 0) ==  input_ret );
}
@Test
public void test4() {
String operate = "div";
assertTrue(hello.operate(operate, 1, 2) ==  input_ret );
}

        @Test
public void test5() {
String operate = "ranstr";
assertTrue(hello.operate(operate, 1, 2) == 0.5);
}
As this method's return statement is *double*, so decide to use assertTrue to compare the return value with user specified correct value.

Now, let's see a more complex method:
public static String operate(HttpServletRequest request, HttpServletResponse response){
String ope = request.getParameter("operate");
int numOne = 0;
int numTwo = 0;
try{
numOne = Integer.parseInt(request.getParameter("num1"));
numTwo = Integer.parseInt(request.getParameter("num2"));
}catch(Exception e){
return "bad parameter";
}
if(ope != null && !ope.trim().isEmpty()){
if(ope.trim().equals("sum"))
return String.valueOf(numOne + numTwo);
else if(ope.trim().equals("min"))
return String.valueOf(numOne - numTwo);
else if(ope.trim().equals("mul"))
return String.valueOf(numOne * numTwo);
else if(ope.trim().equals("div"))
return String.valueOf(numOne / numTwo);
}
return "bad parameter";
}

Do not like String,Integer,Double,Float, Boolean etc, HttpServletRequest  and HttpServletReponse are complex data type, we must use mockito to build parameter instances. We can get test case like this:
@Test
public void test() {
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getParameter("operate")).thenReturn("sum");
when(request.getParameter("num1")).thenReturn("1");
when(request.getParameter("num2")).thenReturn("2");
HttpServletResponse response = mock(HttpServletResponse.class);
assertEquals(hello.operate(request, response), "correct_value_specified_by_user");

We use *mock* function to build HttpServletRequest and HttpServletResponse instance, as aimed test method invoke  request.getParameter three times(request.getParameter("operate"), request.getParameter("num1") and request.getParameter("num2")) to get different parameter value, we use when(...).thenReturn(...) to simulate request, and use assertEquals to compare String return statement with user specified correct value.


This is just *MY PRELIMINARY IDEA*, i hope we can know what i mean. If you guys are interested, i will start the detailed project proposal prepare job soon :-)

2012/2/16 Marcel Bruch <bruch@xxxxxxxxxxxxxxxxxx>
Hi Chen,

good to know it works for you. I think, we should clean up the repository a bit more to reduce confusion. It's on my todo list now

On 16.02.2012, at 10:07, chen cheng wrote:

If we want to write unit test for it, we must build an special HttpServletRequest object for each test case, it will cost us much time. If Recommenders can help us to create unit test file, test cases and build parameter Class object. It is really cool,practical and useful, save time, save money.

How do you think ? Thank you :-)

Sounds interesting. Can you give more details on what exactly you'd like to create? A few examples explaining what you have in mind (maybe using the HttpServletRequest example) would be great. Another aspect: how would this relate to mocking frameworks such as mockito where you create simple mocks using Mockito.mock(class)?

Best,
Marcel

_______________________________________________
recommenders-dev mailing list
recommenders-dev@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/recommenders-dev




--
Best Regards From Cheng Chen [chengchendoc@xxxxxxxxx]
_______________________________________________
recommenders-dev mailing list
recommenders-dev@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/recommenders-dev


Back to the top