2 title: 'Writing Skia Tests'
3 linkTitle: 'Writing Skia Tests'
6 We assume you have already synced Skia's dependencies and set up Skia's build
9 <!--?prettify lang=sh?-->
11 python3 tools/git-sync-deps
13 bin/gn gen out/Release --args='is_debug=false'
15 ## Writing a Unit Test
17 1. Add a file `tests/NewUnitTest.cpp`:
19 <!--?prettify lang=cc?-->
24 * Use of this source code is governed by a BSD-style license
25 * that can be found in the LICENSE file.
28 DEF_TEST(NewUnitTest, reporter) {
30 ERRORF(reporter, "%d + %d != %d", 1, 1, 2);
32 bool lifeIsGood = true;
33 REPORTER_ASSERT(reporter, lifeIsGood);
36 2. Add `NewUnitTest.cpp` to `gn/tests.gni`.
38 3. Recompile and run test:
40 <!--?prettify lang=sh?-->
43 out/Debug/dm --match NewUnitTest
45 ## Writing a Rendering Test
47 1. Add a file `gm/newgmtest.cpp`:
49 <!--?prettify lang=cc?-->
54 * Use of this source code is governed by a BSD-style license
55 * that can be found in the LICENSE file.
58 DEF_SIMPLE_GM(newgmtest, canvas, 128, 128) {
59 canvas->clear(SK_ColorWHITE);
62 canvas->drawLine(16, 16, 112, 112, p);
65 2. Add `newgmtest.cpp` to `gn/gm.gni`.
67 3. Recompile and run test:
69 <!--?prettify lang=sh?-->
72 out/Debug/dm --match newgmtest
74 4. Run the GM inside Viewer:
76 <!--?prettify lang=sh?-->
78 ninja -C out/Debug viewer
79 out/Debug/viewer --slide GM_newgmtest
81 ## Writing a Benchmark Test
83 1. Add a file `bench/FooBench.cpp`:
85 <!--?prettify lang=cc?-->
90 * Use of this source code is governed by a BSD-style license
91 * that can be found in the LICENSE file.
93 #include "Benchmark.h"
96 class FooBench : public Benchmark {
99 virtual ~FooBench() {}
101 const char* onGetName() override { return "Foo"; }
102 SkIPoint onGetSize() override { return SkIPoint{100, 100}; }
103 void onDraw(int loops, SkCanvas* canvas) override {
104 while (loops-- > 0) {
105 canvas->drawLine(0.0f, 0.0f, 100.0f, 100.0f, SkPaint());
110 DEF_BENCH(return new FooBench;)
112 2. Add `FooBench.cpp` to `gn/bench.gni`.
114 3. Recompile and run nanobench:
116 <!--?prettify lang=sh?-->
118 ninja -C out/Release nanobench
119 out/Release/nanobench --match Foo