https://flavorsrecipes.blogspot.com/?m=1 be more attractive: How to Test Drupal Code

lundi 30 mai 2016

How to Test Drupal Code

This article will guide you in starting to write tests for your Drupal code. We'll assume that the code you're writing is primarily within a custom or 'contrib' (community-contributed) Drupal module.There are many ways of testing a Drupal website, but the most popular ways to test Drupal code is unit testing and integration testing. This article will cover unit testing.The easiest way to write and execute tests in Drupal 7 and Drupal 8 is using the Simpletest module, which this guide will cover. However, note that the Drupal core developers have started to use a different testing framework - PHP Unit - for Drupal 8 development.

EditSteps

  1. Enable the Simpletest module.
  2. Look through the tests already available on your site. In any Drupal website, all core modules and many contrib modules that are installed will already have many tests written for Simpletest. You can see all of them by going to 'Configuration' > 'Development' > 'Testing'. Looking through them should give you a better idea of what you can test and how you can label your tests.
  3. Try out the tests that are already available.
  4. Get your module ready for writing tests. In your module folder, add a file named 'modulename.test'. This is a PHP file that you should add all your tests to. Then in your .info file, tell Drupal about the file using files[] = modulename.test.
  5. Add your first test case class.
    <?php
    /**
     * @file
     * Description of file.
     */
     
    class ModuleNameUnitTestsTestCase extends DrupalUnitTestCase {
     
      public static function getInfo() {
        // Note: getInfo() strings should not be translated.
        return array(
          'name' =>         'Functions tests',
          'description' =>  'Test that various functions work properly.',
          'group' =>        'Module Name',
        );
      }
     
    }
    
  6. Write some testing code. Imagine that you've written this function, and you'd like to test that it works properly:
    function negate($int) {
      return $int * -1;
    }
    

    You could write a test (in your modulename.test file, after the getInfo() method) like this:

    public function testNegates() {
      $result = negate(5);
    }
    
  7. Write an assertion that checks the output of the testing code. At the moment, the method in the previous step doesn't actually check the result of the test in any way. To make sure that the value is what we expect, we write an assertion. In this case, we'll add assertEqual() to our method to check that the result equals -5. Also, we'll add a message and group to our assertion, which will be helpful when we're running the test later.
    public function testNegates() {
      $message = "negate() should return a negative integer.";
      $group = "Calculation";
      $result = negate(5);
     
      $this->assertEqual($result, -5, $message, $group);
    }
    
  8. Run your first test. In Drupal, go back to the Testing page. Among the list of tests, you should see the name of your module (if 'group' in your getInfo() method equals your module name). If you don't see it, you might need to flush the Drupal cache (specifically, the 'class' cache). Now enable your test using the checkbox, and select 'Run tests'. After a few seconds, you should see your test, and the result! It should be red if the test failed, and green if it succeeded.
  9. Learn more about Simpletest. You can learn more about the available assertions on the DrupalTestCase page in the Drupal API.

EditTips

  • Test early. As soon as you write code that adds new functionality or fixes broken functionality, write a test to make sure it works as expected.
  • Test often. Keep writing tests as you write code so that it becomes a part of your process, and doesn't become a chore you leave to the end and ignore.
  • Test automatically. If possible, use tools that allow your tests to be run regularly without you having to run the tests yourself.
  • Testing ain't done 'till all the tests run. Making sure you code passes all your tests before handing it off to a client or users means it's far less likely you'll have to come back and fix things later, and it'll improve your reputation and confidence at the same time.

EditSources and Citations



source How to of the Day http://ift.tt/1UoQQbQ

Aucun commentaire:

Enregistrer un commentaire

https://warriorplus.com/o2/a/x8g6yk/0

How to React to a Gift You Do Not Like

Respond to unwanted gifts with grace and tact Your great aunt knitted you the world's ugliest sweater. Your friend got you a CD by a b...

https://warriorplus.com/o2/a/x8g6yk/0