Writing tests that are easy to understand, maintain, and actually tell you something when they fail?
Yes, it’s possible. And it starts here.
Here’s a no-bullshit guide to test automation best practices.
🎯 1. One test = one purpose
A test isn’t a buffet.
Keep it focused. One feature, one scenario, one outcome.
❌ Bad:
def test_login_and_change_password_and_logout():
...
✅ Good:
def test_login_successful():
...
def test_password_change():
...
🔍 2. Use names that actually mean something
If your test is called test1
, you should feel bad.
Tests are code. And code should communicate.
Name your tests like they matter:
def test_login_fails_with_invalid_credentials():
...
🧪 3. Use fixtures, don’t copy-paste setup
Copy-pasting setup code is how your test suite becomes a dumpster fire.
@pytest.fixture
is your cleanup crew. Use it.
🪓 4. Hardcoded values? Just don’t.
Hardcoded URLs, passwords, selectors – future-you hates this.
Use config files, environment variables, or parametrize.
🧱 5. Keep structure: Arrange – Act – Assert
Don’t write tests like you’re playing darts drunk.
Structure it.
# Arrange
driver.get("https://example.com")
# Act
login(driver, "user", "pass")
# Assert
assert "Welcome" in driver.page_source
🧼 6. Clean up (yes, really)
Leaving browser instances open is a crime.
Use yield
in fixtures. Call driver.quit()
.
Be a decent dev.
🛠️ 7. Use waits smartly, skip the sleeps
time.sleep(5)
is for amateurs.
Use WebDriverWait and proper retry logic. Your tests will thank you later.
🧙♂️ 8. Write tests like someone else will read them
…because someone will.
Or you, six months from now.
Readable code. Occasional comment. Don’t be cryptic.
🐍 9. Stick to conventions
test_
prefix, readable assertions, clear structure – just follow the rules. Pytest has them for a reason.
🧩 10. Test what matters
Don’t test the “OK” button just because it’s there.
Ask yourself: “What’s the risk if this breaks?”
If the answer is meh – skip it.
TL;DR
✅ One test, one purpose
✅ Names matter
✅ Setup = fixtures
✅ No hardcoded garbage
✅ Tests = readable code
Write tests you won’t curse at later.
Next up: Page Object Model – how to do it right without turning it into a hydra.
Stay sharp, test sharper 🔥