Loading…

Golden Master Snapshot Testing

Most report and statistics tools in this catalogue - a drawdown report, a trade journal dump, a correlation matrix printout - produce output that is too free-form for a handful of hand-written assert() checks to meaningfully cover. Predicting every specific number or line that a future code change might alter, in advance, is impractical; a bug that shifts a percentage by one decimal place or drops a line entirely is easy to miss by eye and easy to never think to assert on specifically. Golden-master (also called "approval") testing sidesteps writing those assertions by hand. GmRunOrUpdate is called with the tool's own current output and a name: the first time it runs (or whenever the caller passes updateSnapshot=true), the output is written to a plain text file as the "blessed" version. Every later call instead reads that file back and compares it to the new output line by line, reporting the count of differing lines and the exact text of the first mismatch. Any future change to the code that alters the output at all - a changed number, a reordered line, an extra or missing line - shows up immediately as a concrete diff, without anyone having had to predict which characters might change ahead of time. When a change is deliberate, re-running with updateSnapshot=true re-blesses the new output as correct. Notes and limitations - The snapshot file is plain text, one line per array element - open it in any text editor to review exactly what was blessed, or delete it to force a fresh snapshot on the next run. - Comparison is exact, line by line - no fuzzy matching, no tolerance for floating-point formatting differences. A report that formats the same number with a different number of decimal places between two runs will register as a mismatch even if the underlying value did not change; keep number formatting in the report itself consistent and deterministic. - GmRunOrUpdate silently creates the snapshot on the very first run (no prior snapshot to compare against) - this is intentional bootstrapping behaviour, not a false "pass". Always inspect a brand new snapshot file by eye once before trusting later comparisons against it. - Not a full test framework - there is no test registry, no pass/fail exit code, and no assertion beyond the line-by-line diff itself. It is one building block (capture + compare) meant to be called from whatever test-running script or EA already drives the tool being snapshotted.

Script · MT5 · v1.0 · Free

About Golden Master Snapshot Testing

Golden Master Snapshot Testing is a script for MetaTrader 5. It has 1 setting, such as Update snapshot. MQL source code included.

Files in the download: GoldenMasterTesting.mq5.mqh, GoldenMasterTesting.txt, GoldenMasterTestingDemo.ex5, GoldenMasterTestingDemo.mq5.

Settings

SettingDefaultWhat it does
Update snapshotfalsetrue = bless the current output as the new snapshot

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria