QUnit test order

I created a website with jQuery and a lot of ajax requests (json format).
I would like to do a few unit test to test the request on the server side.
Since I used jQuery, I use qUnit, but I have a problem with the testing order ...

For example, I wanted to check this: - create a user => it may be possible
- it is possible to rename a user with a valid name =>
- it is impossible to rename a user using a name =>.
- delete user => could

My code is:

  $("button#test").button().click(function() {
    module("Module Users");
    newName = 'newUserName';
    userId = 0;

    test("1 Add a user", function() {
      stop();
      $.getJSON(Request,{'action':'add','table':'users'}
        ,function(data) {
          equal( data.status,"OK", "Answer is OK" );
          notEqual( data.item,null, "item is return" );
          userId = data.item.id;
          start();
      });
    });

    test("2 Rename user", function() {
      stop();
      $.getJSON(Request,{'action':'modify','table':'users','id':userId,'field':'name','value':newName}
        ,function(data) {
          equal( data.status,"OK", "Answer is OK" );
          equal( data.value,newName, "Return value is OK" );
          start();
      });
    });

    test("3 Rename user with use name", function() {
      stop();
      badName = 'usedName'; // assert that a user with this name exists
      $.getJSON(Request,{'action':'modify','table':'users','id':userId,'field':'name','value':badName}
        ,function(data) {
          equal( data.status,"Fail", "Answer is Fail" );
          equal( data.value,newName, "Return value is previous name" );
          start();
      });
    });

    test("4 Remove the user", function() {
      stop();
      $.getJSON(Request,{'action':'remove','table':'users','id':userId}
        ,function(data) {
          equal( data.status,"OK", "Answer is OK" );
          start();
      });
    });

But the problem is that 1 test is performed, then 4 and 2 and 3 ... (Then, I think the problem is that my tests are not independent)

?
4 1, , ...

?

+3
3

kelloti, qUnit " "... .

-4

, .

QUnit.config.reorder = false;

+8

The main problem in your example is that the tests are executed in the click event handler. You need to reorganize this and make test () calls at the top level (regardless of any click event). Since your tests only allow you to test the ajaxy function, you do not need to use the button at all. So something like this:

test("1 Add a user", function() {
  stop();
  $.getJSON(Request,{'action':'add','table':'users'}
    ,function(data) {
      equal( data.status,"OK", "Answer is OK" );
      notEqual( data.item,null, "item is return" );
      userId = data.item.id;
      start();
  });
});

test("2 Rename user", function() {
  stop();
  $.getJSON(Request,{'action':'modify','table':'users','id':userId,'field':'name','value':newName}
    ,function(data) {
      equal( data.status,"OK", "Answer is OK" );
      equal( data.value,newName, "Return value is OK" );
      start();
  });
});

test("3 Rename user with use name", function() {
  stop();
  badName = 'usedName'; // assert that a user with this name exists
  $.getJSON(Request,{'action':'modify','table':'users','id':userId,'field':'name','value':badName}
    ,function(data) {
      equal( data.status,"Fail", "Answer is Fail" );
      equal( data.value,newName, "Return value is previous name" );
      start();
  });
});

test("4 Remove the user", function() {
  stop();
  $.getJSON(Request,{'action':'remove','table':'users','id':userId}
    ,function(data) {
      equal( data.status,"OK", "Answer is OK" );
      start();
  });
});
+1
source

Source: https://habr.com/ru/post/1790495/


All Articles