Damn, I spent two hours discovering the reason of my Selenium test failing.
It turned out that the title of the web page contains non-breaking spaces instead of common ones. I wondered why "s.indexOf(s1)" returns -1, where:
s = "blablablb :: Directions";
s1 = ":: Directions";
Tuesday, January 29, 2008
Parameters & JUnit Test Suites
There appeared another one problem with JUnit I faced. My task consisted in composing a test suite from tests that take the same parameters set (with different values, sure).
Let's imagine our test suite like this:
TestSuite suite = new TestSuite();
suite.add(testClass1);
suite.add(testClass2);
suite.add(testClass3);
and you need to pass two parameters to each test class:
// testClass1
(param11, param12)
// testClass2
(param21, param22)
// testClass3
(param31, param32)
As I understand, JUnit doesn't let you any means to do it, but you can get out from this situation using parameterized tests (see the previous post).
Picture 1 shows what is supposed to be done.

Picture 2 shows what you can attempt to do to solve this limitation.
Let's imagine our test suite like this:
TestSuite suite = new TestSuite();
suite.add(testClass1);
suite.add(testClass2);
suite.add(testClass3);
and you need to pass two parameters to each test class:
// testClass1
(param11, param12)
// testClass2
(param21, param22)
// testClass3
(param31, param32)
As I understand, JUnit doesn't let you any means to do it, but you can get out from this situation using parameterized tests (see the previous post).
Picture 1 shows what is supposed to be done.
Picture 2 shows what you can attempt to do to solve this limitation.
Parameters & JUnit Tests
I ran across one trouble with Junit tests recently. The point is that you can't pass parameters to JUnit tests directly (only using external storages such as file system objects, databases etc, but it's not always the best way for this), to be more precise you couldn't until JUnit 4. Besides new syntax technics, now it supports so called "parameterized tests" - this means you write a test once and it's possible to run it with a set of defined parameters. Before, you should write a particular test method for each parameter.
For example, you got a class that has a method summing two numbers and you wanna test it using 3 different parameters.
Before, you had to write three separate JUnit methods, it looked something like this:
public TestClass extends TestCase {
public void setUp() {
}
public void test1() {
assertEquals(new MyClass.sum(1, 2), 3);
}
public void test2() {
assertEquals(new MyClass.sum(3, 4), 7);
}
public void test3() {
assertEquals(new MyClass.sum(5, 6), 11);
}
public void tearDown() {
}
}
Since JUnit 4 you can do this simpler:
@RunWith(Parameterized.class)
public TestClass {
int a;
int b;
int res;
// Here you describe a method that returns parameters you wanna use to test "sum()" method
@Parameters
public static Collection getParameters() {
return Arrays.asList(new Object[][] = {{1, 2, 3}, {3, 4, 7}, {5, 6, 11}});
}
public TestClass(int a, int b, int res) {
this.a = a;
this .b = b;
this.res = res;
}
// implements the test method
@Test
public void sumTester() {
assertEquals(new MyClass.sum(a, b), res);
}
}
So, for making parameterized test work, you need:
1) describe method that returns parameters (using @Parameters javadoc)
2) implement public constructor that takes the parameters (source data + probably a right result to make an assert) and saves them into private fields.
3) implement test method
For example, you got a class that has a method summing two numbers and you wanna test it using 3 different parameters.
Before, you had to write three separate JUnit methods, it looked something like this:
public TestClass extends TestCase {
public void setUp() {
}
public void test1() {
assertEquals(new MyClass.sum(1, 2), 3);
}
public void test2() {
assertEquals(new MyClass.sum(3, 4), 7);
}
public void test3() {
assertEquals(new MyClass.sum(5, 6), 11);
}
public void tearDown() {
}
}
Since JUnit 4 you can do this simpler:
@RunWith(Parameterized.class)
public TestClass {
int a;
int b;
int res;
// Here you describe a method that returns parameters you wanna use to test "sum()" method
@Parameters
public static Collection getParameters() {
return Arrays.asList(new Object[][] = {{1, 2, 3}, {3, 4, 7}, {5, 6, 11}});
}
public TestClass(int a, int b, int res) {
this.a = a;
this .b = b;
this.res = res;
}
// implements the test method
@Test
public void sumTester() {
assertEquals(new MyClass.sum(a, b), res);
}
}
So, for making parameterized test work, you need:
1) describe method that returns parameters (using @Parameters javadoc)
2) implement public constructor that takes the parameters (source data + probably a right result to make an assert) and saves them into private fields.
3) implement test method
Sunday, January 6, 2008
Sony Ericsson "Track ID" feature
My phone broke once and for all and so I decided to buy a new one. I used LG, 2 Motorola's, Siemens, Nokia before - I prefer to buy different brands every time. Well, they say Samsung's suck, Philips-Voxtel-Sagem didn't seem to be my choice, therefore I shot a glance at SonyEricsson, the more so as I heard a lot of favourable reports about 'em. Sure, I browsed several e-shops sites, read some comments ( by the way, I hadn't thought that comments could have been so useful before :) ) and bought k550i model at last. Well, we came to the point what do I write this for. SE K550i got one wonderful feature called "track id". It lets you a possibility to identify music you listen to, for example, say, you heard a really nice track on the radio going to the work, just choose "track id" function, the phone will record a small song fragment, send it to some server and receive a response.
I don't know how it works so fast, but it actually does.
Ok, I understand that there exist different noise-suppressing algorithms (heh, my first uni exam "Digital Signals Processing" is coming soon). But I wonder how it finds info so rapidly.
Let's imagine that a track snatch is just an array of integers. That is, we got a lot of source integer arrays (tracks) and we got an array (much less) we need to identify, moreover considering FM signal is far from being ideal, so the array need to be identified is not necessary a subset of one of the source arrays (supposing target track is contained in DB).
Any ideas?
I don't know how it works so fast, but it actually does.
Ok, I understand that there exist different noise-suppressing algorithms (heh, my first uni exam "Digital Signals Processing" is coming soon). But I wonder how it finds info so rapidly.
Let's imagine that a track snatch is just an array of integers. That is, we got a lot of source integer arrays (tracks) and we got an array (much less) we need to identify, moreover considering FM signal is far from being ideal, so the array need to be identified is not necessary a subset of one of the source arrays (supposing target track is contained in DB).
Any ideas?
Saturday, January 5, 2008
Blogging start
Hm, it's really my first post in internet space. I've finally considered all pro's and con's of writing own blog and decided to start. Sure it takes some amount of time on the one hand, and due to a lot of work, study and ... yep, my girlfriend, I'm supposed to say "No, it's not for me". However, sometimes there appear thoughts you want to share with someone other... not because of loneliness... it's just because sometimes there occur situations you should to tell to probably pretend someone to get into. It's something about this =) I hope you got what I'm saying... I really do.
Subscribe to:
Posts (Atom)