--- title: Writing Good Tests description: Worked examples of well-written tests across common testing patterns. --- # Writing Good Tests These examples illustrate the patterns that produce reliable, consistent runs. ## Login test Name the button you're clicking. "Clicks **Sign in**" is better than "logs in." For credentials, use [environments](/api/environments) instead of hardcoding values directly in the test — this keeps secrets out of your test text and makes it easy to run the same test against multiple environments. {`User navigates to the login page at /login. Enters the email stored in the environment variable TEST_EMAIL in the email field. Enters the password stored in TEST_PASSWORD in the password field. Clicks the "Sign in" button. Expected outcome: The user lands on the dashboard page. The heading "Welcome back" is visible in the top-left.`} ## Form submission {`User opens the contact page at /contact. Fills in the "Name" field with "Jane Smith". Fills in the "Email" field with "jane@example.com". Types "I have a question about pricing." in the "Message" field. Clicks the "Send message" button. Expected outcome: A success banner appears with the text "Thanks for reaching out! We'll be in touch soon." The form fields are cleared.`} ## Navigation check Check for visible UI elements, not URLs — the agent is looking at the page, not your router. {`User opens the homepage. Clicks "Docs" in the top navigation bar. Expected outcome: The documentation page loads. The heading "Getting started" is visible on the page. The URL contains "/docs".`} ## Content verification {`User opens the blog index at /blog. Clicks on the first article in the list. Expected outcome: An article page opens. A heading is visible at the top. The article body text is present (at least a few paragraphs). The author name and publish date are displayed.`} ## Authenticated state check {`User signs in using the credentials in TEST_EMAIL and TEST_PASSWORD. Then navigates to /account/settings. Expected outcome: The account settings page is visible. The user's email address is displayed in the profile section. There is no redirect to the login page.`} Use an [environment variable set](/api/environments) to store test credentials. This avoids hardcoding emails and passwords in your test text, and lets you run the same test against staging and production by switching environments. ## Checkout test (partial) {`User opens the pricing page at /pricing. Clicks "Get started" on the Pro plan card. Expected outcome: The checkout page opens. The plan name "Pro" and the monthly price are visible. The "Complete purchase" button is present.`} ## Error state verification {`User navigates to the login page at /login. Enters "wrong@example.com" as the email. Enters "badpassword" as the password. Clicks "Sign in". Expected outcome: An error message is shown on the page. The text contains "Invalid credentials" or similar. The user remains on the login page.`}