Opened 11 years ago

Closed 7 years ago

Last modified 7 years ago

#6959 closed enhancement (wontfix)

[patch][cla] simplified doh testing

Reported by: guest Owned by: Tom Trenka
Priority: low Milestone: future
Component: TestFramework Version: 1.1.1
Keywords: needsreview Cc:
Blocked By: Blocking:

Description (last modified by bill)

I posted a note on the discussion forum about this, but without response. I really think that a very simple addition to D.O.H., that could be completely optional in its use and would not affect any existing tests, would be a huge benefit to encouraging unit testing and simplifying how they work, and to making them feel more "junit" like.

I have built a base class for "test fixtures" that are already used by D.O.H. It takes advantage of the runTest, setUp, tearDown tests, and is used as follows:

For synchronous testing:

dojo.declare("MySynchronousTestFixture", test.TestFixture, {
   setUp:function() {
      this.someInitializedVariable = "some initialized variable";
      this.someInitializedObject = {value:"initialized"};
   }
});

Then you register your tests as usual, but if you declare your test using the MySynchronousTestFixture? class, then you get a lot of functions for free. First off, every time you run a test, you get the same setUp method to run, which can be very helpful. Secondly, you get to use all the properties declared in your setUp method (or elsewhere for the fixture) as local variables for the test. Thirdly you get your assertions without any prefixes, as follows:

doh.register("myTests",[
   new MySynchronousTestFixture("firstTest", function() {
      assertEqual("some initialized variable", someInitializedVariable, "someInitializedVariable should already be initialized!");
      assertNotNull(someInitializedObject, "the object should have already been initialized, and should never be null");
      assertNotDefined(someInitializedObject.someUndefinedProperty);
   });
]);

This is extended to include very simple support for asynchronous tests:

dojo.declare("MySynchronousTestFixture", test.AsyncTestFixture, {
   setUp:function() {
      this.someInitializedVariable = "some initialized variable";
      this.someInitializedObject = {value:"initialized"};
   }
});

and usage is really a snap:

doh.register("myTests",[
   new MySynchronousTestFixture("firstTest", function() {
      return doSomethingThatReturnsAnyKindOfDeferred(someInitializedObject);
   }, function(result) {
	  assertEqual("TEST NEW PROPERTY", someInitializedObject.newProperty, "the newProperty should have been set by now");
   });
]);

and if you want to test something asynchronous that does not return a deferred, its still very easy:

doh.register("myTests",[
   new MySynchronousTestFixture("firstTest", function() {
      doSomethingThatTriggersMyCallback(callback); //callback is a "magic" word that needs to be assigned when not using a deferred
   }, function(result) {
	  assertEqual("TEST NEW PROPERTY", someInitializedObject.newProperty, "the newProperty should have been set by now");
   });
]);

Perhaps this is just a stylistic issue, but I think these tests are a lot easier to read and to write, with less of a learning curve to figure out how to do asynchronous testing. And its all just added bonus.

I've attached the code for test.TestFixture and test.AsyncTestFixture. I hope you will consider adding this, or something like this to doh.

Attachments (1)

TestFixture.js (5.7 KB) - added by guest 11 years ago.
code for creating a test fixture to simplify doh unit testing

Download all attachments as: .zip

Change History (15)

Changed 11 years ago by guest

Attachment: TestFixture.js added

code for creating a test fixture to simplify doh unit testing

comment:1 Changed 11 years ago by guest

woops, didn't leave anyway of contacting me --

-maulin shah -http://blog.medryx.org

comment:2 Changed 11 years ago by alex

Status: newassigned

I like it!

A couple of thoughts:

  • have you sent in a CLA? If so, we can get the patch integrated
  • would be good if you could use documentation comments in the style which Dojo's API tool parses. Guidelines for that are here: http://dojotoolkit.org/developer/StyleGuide#Documentation
  • it would be good if the classes had a markupFactory() method or if their constructors accepted the properties/node pair which Dijit widgets do so that you could write your unit tests in markup (I know, perhaps an edge-case, but wouldn't it be sweet?).

comment:3 in reply to:  2 Changed 11 years ago by guest

  1. Just faxed the CLA
  2. Will clean up the code a bit and comments, then post again here?
  3. I've also got unit tests of the test fixture itself, should i post those here as well?
  4. Markup is an interesting thought. I'll have to mull it over a bit to wrap my head around how it would work.

Something like this?

Or even:

?? Just a thought. Although I'd want to think through my actual use case, and how it might run before building that bit.

Sorry for the silly questions. Its my first submission!

-maulin http://blog.medryx.org

comment:4 Changed 11 years ago by guest

arg -- html wikiformatting swallowed my markup.

Something like this?

<div dojoType="doh.TestFixture?">

<div dojoType="some.widget.to.Test" id="testWidgetId" />

<script type="dojo/javascript">

assertTrue(somethingIsTrue);

<script>

</div>

<div dojoType="doh.TestFixture?">

<div dojoType="some.widget.to.Test" id="testWidgetId" />

<div dojoType="doh.Assertion" assert="1===1" />

</div>

comment:5 Changed 11 years ago by dylan

Summary: simplified doh testing[patch][cla] simplified doh testing

comment:6 Changed 11 years ago by bill

Milestone: 1.21.3

bump enhancements to next milestone, as we prepare to close out 1.2

comment:7 Changed 11 years ago by bill

Milestone: 1.3future

comment:8 Changed 9 years ago by Chris Mitchell

Owner: changed from alex to dylan

please review/triage

comment:9 Changed 8 years ago by bill

Description: modified (diff)

comment:10 Changed 8 years ago by bill

Seems like with() would be better than eval() as the way to make those new local variables like assertTrue().

comment:11 Changed 8 years ago by ben hockey

Keywords: needsreview added

comment:12 Changed 8 years ago by dylan

Owner: changed from dylan to Tom Trenka

Tom, perhaps something for you to consider with your work on DOH?

comment:13 Changed 7 years ago by Kitson Kelly

Resolution: wontfix
Status: assignedclosed

While this is a good idea, I am afraid that this change won't make it for Dojo 1.9 and for Dojo 2.0 we are heading a different direction with the testing framework (which will incorporate some of the concepts here). So closing at wontfix.

comment:14 Changed 7 years ago by dylan

https://github.com/csnover/dojo2-teststack is the current leading contender for the test framework for Dojo 2.0. If you want to get involved with that project (if you're still interested), that would be very welcome and appreciated.

Note: See TracTickets for help on using tickets.