• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Unit testing Cocoa-Java applications?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Unit testing Cocoa-Java applications?


  • Subject: Re: Unit testing Cocoa-Java applications?
  • From: Chris Hanson <email@hidden>
  • Date: Sun, 18 May 2003 01:19:05 -0500

Here's an update: I was just able to run some unit tests with JUnit in a Cocoa-Java application. This email explains how I do it.

Here's the method I call from my application's delegate's applicationDidFinishLaunching() method:

public void runAllUnitTestsIfRequested()
{
Boolean runAllUnitTests;

runAllUnitTests = new Boolean(
(String) NSSystem.environment().valueForKey("RunAllUnitTests"));

if (runAllUnitTests.booleanValue() == true) {
try {
// This is the same as
// TestAll.runAllUnitTests();
// only it's fully dynamic, and will fail
// safely in the release build.

Class testAllClass = Class.forName("TestAll");

Method runAllUnitTestsMethod =
testAllClass.getMethod("runAllUnitTests", null);

runAllUnitTestsMethod.invoke(null, null);
}

catch (Exception classNotFound) {
// Do nothing
}
}
}

This means unit tests will only be run in the test build of the application that links against /Library/Extensions/junit.jar and contains all of my test cases, *and* only if the RunAllUnitTests environment variable is set to "true".

The class TestAll extends junit.framework.TestCase. It has a static suites() method that returns a test suite containing all of the test suites in the application, and a static runAllUnitTests() method that uses junit.textui.TestRunner to run all of the test suites and send their results to stdout.

I don't have it running tests automatically as part of the build yet. That should be a simple matter of writing a simple shell script build phase to set the environment variable appropriately, launch the application, and then terminate it once the test suite is done executing.

On the whole, JUnit isn't as nice as OCUnit out of the box since it doesn't do things like automatically discover test suites and report in a format Project Builder 2.1's build window can interpret. But it's serviceable, and a lot better than having no unit tests.

-- Chris

--
Chris Hanson, bDistributed.com, Inc. | Email: email@hidden
Custom Application Development | Phone: +1-847-372-3955
http://bdistributed.com/ | Fax: +1-847-589-3738
http://bdistributed.com/Articles/ | Personal Email: email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Unit testing Cocoa-Java applications?
      • From: Chris Hanson <email@hidden>
References: 
 >Unit testing Cocoa-Java applications? (From: Chris Hanson <email@hidden>)

  • Prev by Date: Re: reading a text file
  • Next by Date: [Newbie] Do I need to learn Java to create Audio Unit plug-ins?
  • Previous by thread: Unit testing Cocoa-Java applications?
  • Next by thread: Re: Unit testing Cocoa-Java applications?
  • Index(es):
    • Date
    • Thread