Skip to main content

Posts

Showing posts from 2010

Simpler parameterization of Junit tests

Why write a custom runner/suite? Usually Junit's default suite/runner combined with parameterized.class are great for parameterizing your tests. But the need to provide a specifically written constructor, and the need to add a specifically annotated and written method to your test class is rather annoying. The Collection syntax is also weird, though generic. To hide the details of parameterization, and provide such support simply through the use of @RunWith is what I am after. It is okay if my class has to implement another interface, but I would look to minimize impact on the user test class and coupling between test class and test infrastructure. It is good that JUnit 4 has made it easier to write your own custom Suites and Runners. How do suites and runners work? In brief, a Junit Suite wraps around your test class and create a test 'runner' for your class. A Suite can contain one or more test classes, but for design ease, I will always assume one suite contains on

Junit parameterization - the next step

Background I have just been fooling around with Junit's parameterization support. Here is what I learned so far JUnit 4 has support for parameterization. In short, write test points, and run them for different parameters. for eg. The Test point could be test() { launch browser for (browsername); } and it needs to be run for multiple browsers. The Parameter here is the 'browsername'. JUnit allows you to define a @Parameters decorated method (with any name) which you use to define what the list of browser names are. Of course, you can even have multiple parameters (which is why the weird List of array syntax appears) Thus, if you want to run launchbrowser() with 'ie', 'firefox', 'chrome', you write (Note: you need to add @RunWith(Parameterized.class). See Junit4 doc for details. @RunWith(Parameterized.class) public class myTest { @Parameters public static Collection mybrowsers() { // Return a collection of string[]'s here // Weird that JUnit re

Cool web charting with Flot

Flot has become one of my favorite charting tools because of its simplicity, and the fact that it works! I have tried several charting tools, and they don't always work as expected. For example, this tool was the first that worked for me without having to worry about even-ing out my x-axis across all series! It is a forward looking tool, as it uses the HTML5 canvas. For the time being, you have to use an extra js file to make it work with IE. It also has great support for time series data plotting, which is often a pain for most people I used it successfully with a Rails application, and got my chart working in about a week or less. Adding Javascript interaction to turn series on and off was also trivial. Here is roughly how it looked for me I will post an example when I get a chance, but in the meantime, here is the project page http://code.google.com/p/flot/ Enjoy Flot!

Some subquery magic with MySQL

As part of a Rails project I had to gather up some data from a single table, and calculate various stats on the data Problem was - had to group data by a field, then calculate aggregates in various ways not possible with a single GROUP BY query Using the SQL cookbook tips, and some pondering, I learned that I could create a bunch of 'fake' tables from the same table using subqueries, then join these tables in a way that would yield a single resultset An example: Let us say you have a table of person, their test points whether they passed the test attempt, like (two people a and b tried a test at various times) : Name Points Passed a 10 1 a 25 0 b 21 1 a 28 0 b 23 1 a 35 1 b 31 1 a 35 1 b 41 1 You want a result like Name Avg Age No. of attempts'passed' a 30.3 5 b 25.1 4 (caution: These numbers are probably wrong just too lazy to fix them up!) As you can see, it is not possible with a single query, since grouping by name and