Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce unit test based on image comparison #3988

Merged
merged 1 commit into from Mar 5, 2017

Conversation

simonbrunel
Copy link
Member

@simonbrunel simonbrunel commented Mar 5, 2017

Attempt to make easier the creation of unit tests that check the drawing output. Until now, this was done by checking calls on a 'fake' context, which is hard to maintain (need to update pixel values by hands) and also not reliable when optimizing code (i.e. different calls sequence but same result).

As of now, it's possible to define 'auto' tests based on JSON/PNG fixtures: chart is generated from the JSON file and compared to the associated PNG image. The image diff is done using pixelmatch. As an example (and in preparation of the filler plugin), add auto tests for the line element fill options.

Creating image based unit tests

  • Create a JS file or JSON file (similar to this one) that defines chart config1 and generation options.
  • Add this file intest/fixtures/{spec.name}/{feature-name}.json.
  • Make sure this kind of line is at the beginning of specs/{spec.name}.js.
  • Set "debug": true in the JSON file to prevent the canvas destruction when running tests.
  • Run gulp unittest --watch --inputs=test/specs/{spec.name}.js.
  • Click the "Debug" button (top/right): a test should fail with the associated canvas visible.
  • Right click on the chart and "Save image as..." test/fixtures/{spec.name}/{feature-name}.png.
  • Refresh the browser page (CTRL+R): test should now pass2.
  • Verify test relevancy by changing the feature values slightly in the JSON file.
  • Remove debug: true from the JSON file.

1 Important: generated charts should be as minimal as possible and focus only on tested features to prevent failure if another feature breaks (e.g. disable the title and legend when testing scales).

2 Tests should pass in both browsers, if not, tolerance and or threshold need to be adjusted at the beginning of the JSON file and kept as low as possible.

Attempt to make easier the creation of unit tests that check the drawing output. Until now, this was done by checking calls on a 'fake' context, which is hard to maintain (need to update pixel values by hands) and also not reliable when optimizing code (i.e. different calls sequence but same result).

As of now, it's possible to define 'auto' tests based on JSON/PNG fixtures: chart is generated from the JSON file and compared to the associated PNG image. The image diff is done using `pixelmatch`. As an example (and in preparation of the `filler` plugin), add auto tests for the line element `fill` options.
@simonbrunel
Copy link
Member Author

simonbrunel commented Mar 5, 2017

Example of a failing test (spanGaps: false instead of true):

image

@tannerlinsley tannerlinsley merged commit 1ca0ffb into chartjs:master Mar 5, 2017
@tannerlinsley
Copy link
Contributor

Spectacular!!

@GabrielMancik
Copy link
Contributor

I am trying to create new test. But the canvas seems to be destroyed when running tests so i can not create png file. I assume "debug": true should be put inside test/fixtures/{spec.name}/{feature-name}.json. But I have try putting it everywhere there without success.

json: scale-suggestedMin-suggestedMax-no-values.txt is located in test\fixtures\scale.linear

test\specs\scale.linear.tests: scale.linear.tests.txt

roicos pushed a commit to roicos/Chart.js that referenced this pull request Aug 21, 2017
Attempt to make easier the creation of unit tests that check the drawing output. Until now, this was done by checking calls on a 'fake' context, which is hard to maintain (need to update pixel values by hands) and also not reliable when optimizing code (i.e. different calls sequence but same result).

As of now, it's possible to define 'auto' tests based on JSON/PNG fixtures: chart is generated from the JSON file and compared to the associated PNG image. The image diff is done using `pixelmatch`. As an example (and in preparation of the `filler` plugin), add auto tests for the line element `fill` options.
exwm pushed a commit to exwm/Chart.js that referenced this pull request Apr 30, 2021
Attempt to make easier the creation of unit tests that check the drawing output. Until now, this was done by checking calls on a 'fake' context, which is hard to maintain (need to update pixel values by hands) and also not reliable when optimizing code (i.e. different calls sequence but same result).

As of now, it's possible to define 'auto' tests based on JSON/PNG fixtures: chart is generated from the JSON file and compared to the associated PNG image. The image diff is done using `pixelmatch`. As an example (and in preparation of the `filler` plugin), add auto tests for the line element `fill` options.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants