A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developers.google.com/blockly/guides/modify/web/unit-testing below:

Unit tests | Blockly | Google for Developers

Skip to main content Unit tests

Stay organized with collections Save and categorize content based on your preferences.

After changing or adding code, you should run existing unit tests and consider writing more. All tests are executed on the uncompressed versions of the code.

There are two sets of unit tests: JS tests and block generator tests.

JS Tests

The JS tests confirm the operation of internal JavaScript functions in Blockly's core. We use Mocha to run unit tests, Sinon to stub dependencies, and Chai to make assertions about the code.

Running Tests

In both blockly and blockly-samples, npm run test will run the unit tests. In blockly, this will also run other tests such as linting and compilation. You can also open tests/mocha/index.html in a browser to interactively run all mocha tests.

Writing Tests

We use the Mocha TDD interface to run tests. Tests are organized into suites, which can contain both additional sub-suites and/or tests. Generally, each component of Blockly (such as toolbox or workspace) has its own test file which contains one or more suites. Each suite can have a setup and teardown method that will be called before and after, respectively, each test in that suite.

Test Helpers

We have a number of helper functions specific to Blockly that may be useful when writing tests. These can be found in core and in blockly-samples.

The helper functions include sharedTestSetup and sharedTestTeardown which are required to be called before and after your tests (see Requirements section).

The function has one optional options parameter to configure setup. Currently, it's only used to determine whether to stub Blockly.Events.fire to fire immediately (will stub by default).

Test Requirements Test Structure

Unit tests generally follow a set structure, which can be summarized as arrange, act, assert.

  1. Arrange: Set up the state of the world and any necessary conditions for the behavior under test.
  2. Act: Call the code under test to trigger the behavior being tested.
  3. Assert: Make assertions about the return value or interactions with mocked objects in order to verify correctness.

In a simple test, there may not be any behavior to arrange, and the act and assert stages can be combined by inlining the call to the code under test in the assertion. For more complex cases, your tests will be more readable if you stick to these 3 stages.

Here is an example test file (simplified from the real thing).

suite('Flyout', function() {
  setup(function() {
    sharedTestSetup.call(this);
    this.toolboxXml = document.getElementById('toolbox-simple');
    this.workspace = Blockly.inject('blocklyDiv',
        {
          toolbox: this.toolboxXml
        });
  });

  teardown(function() {
    sharedTestTeardown.call(this);
  });

  suite('simple flyout', function() {
    setup(function() {
      this.flyout = this.workspace.getFlyout();
    });
    test('y is always 0', function() {
      // Act and assert stages combined for simple test case
      chai.assert.equal(this.flyout.getY(), 0, 'y coordinate in vertical flyout is 0');
    });
    test('x is right of workspace if flyout at right', function() {
      // Arrange
      sinon.stub(this.flyout.targetWorkspace, 'getMetrics').returns({
        viewWidth: 100,
      });
      this.flyout.targetWorkspace.toolboxPosition = Blockly.TOOLBOX_AT_RIGHT;
      this.flyout.toolboxPosition_ = Blockly.TOOLBOX_AT_RIGHT;

      // Act
      var x = this.flyout.getX();

      // Assert
      chai.assert.equal(x, 100, 'x is right of workspace');
    });
  });
});

Things to note from this example:

Debugging Tests Block Generator Tests

Each block has its own unit tests. These tests verify that blocks generate code than functions as intended.

  1. Load tests/generators/index.html in Firefox or Safari. Note that Chrome and Opera have security restrictions that prevent loading the tests from the local "file://" system (Issues 41024 and 47416).
  2. Choose the relevant part of the system to test from the drop-down menu, and click "Load". Blocks should appear in the workspace.
  3. Click on "JavaScript".
    Copy and run the generated code in a JavaScript console. If the output ends with "OK", the test has passed.
  4. Click on "Python".
    Copy and run the generated code in a Python interpreter. If the output ends with "OK", the test has passed.
  5. Click on "PHP".
    Copy and run the generated code in a PHP interpreter. If the output ends with "OK", the test has passed.
  6. Click on "Lua".
    Copy and run the generated code in a Lua interpreter. If the output ends with "OK", the test has passed.
  7. Click on "Dart".
    Copy and run the generated code in a Dart interpreter. If the output ends with "OK", the test has passed.
Editing Block Generator Tests
  1. Load tests/generators/index.html in a browser.
  2. Choose the relevant part of the system from the drop-down menu, and click "Load". Blocks should appear in the workspace.
  3. Make any changes or additions to the blocks.
  4. Click on "XML".
  5. Copy the generated XML into the appropriate file in tests/generators/.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-06-16 UTC.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-16 UTC."],[[["Blockly has two sets of unit tests: JS tests for internal JavaScript functions and block generator tests for verifying block code generation."],["JS tests are run using Mocha, Sinon, and Chai, and can be executed with `npm run test` or through a browser interface."],["When writing JS tests, follow the arrange, act, assert structure, utilize provided test helpers, and adhere to the outlined test requirements for setup and teardown."],["Block generator tests involve loading predefined blocks or custom XML, generating code in various languages, and then executing that code in the corresponding interpreter to validate its functionality."],["To edit block generator tests, modify blocks in the provided web interface, obtain the generated XML, and update the appropriate file within the tests/generators/ directory."]]],["After coding changes, run unit tests, including JS and block generator tests. JS tests use Mocha, Sinon, and Chai. Run tests with `npm run test` or via `tests/mocha/index.html`. Tests follow an arrange, act, assert structure. Use `sharedTestSetup` and `sharedTestTeardown` helper functions, disposing of any created workspaces. Block generator tests verify code output in JavaScript, Python, PHP, Lua, and Dart interpreters, indicated by \"OK\" output. Edit block tests by modifying them in the workspace, then copy the resulting XML to the relevant file.\n"]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4