this post was submitted on 23 Jul 2024
17 points (100.0% liked)

Learn Programming

1624 readers
1 users here now

Posting Etiquette

  1. Ask the main part of your question in the title. This should be concise but informative.

  2. Provide everything up front. Don't make people fish for more details in the comments. Provide background information and examples.

  3. Be present for follow up questions. Don't ask for help and run away. Stick around to answer questions and provide more details.

  4. Ask about the problem you're trying to solve. Don't focus too much on debugging your exact solution, as you may be going down the wrong path. Include as much information as you can about what you ultimately are trying to achieve. See more on this here: https://xyproblem.info/

Icon base by Delapouite under CC BY 3.0 with modifications to add a gradient

founded 1 year ago
MODERATORS
 

I am really struggling to include proper testing practices into my code and would appreciate any advice on how to get going. I work in web dev so my I am interested in how to properly implement a suite of tests for websites and incorporate into it a CI/CD pipeline.

I find a lot of tutorials teach the most basic types of unit tests, 90% of the time most instructors teach how to write a test to sum two numbers, but when it comes to writing real unit test I find it hard to know what I should be testing. I learnt some cypress and have gotten better at including end-to-end testing because that makes more sense to me, but I still feel I am way short of where I should be.

How can I move forward? Did anyone else find themselves in my situation and find good resources to help them learn? Thx

you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 9 points 1 month ago (1 children)

It's odd that functional testing feels easier to you than unit testing; I find the opposite, and that unit tests force better structure into my code. I write fewer monolithic mega functions and more, smaller, simpler functions that can be unit tested.

Maybe it would help to approach some code in reverse to how you'd normally write it. Test-first development. You write your functional function, but make it full of stubs you think you'll need to call - top-down design. Then, write unit tests for those stubs - they can be simple at first, basically pass if they get some expected result, and fail otherwise. Then run your tests and confirm they all fail. Then, one by one, fill in the stubs, every time running your tests and watching as more and more tests pass.

This approach has a number of benefits:

  • test-first development encourages writing smaller, more easily understood functions
  • test-first development gives you high code coverage, which helps prevent bugs from creeping in during development
  • there's a definite positive feedback endorphin reward from writing watching more tests pass as you develop, which encourages you to keep tests up to date and running clean

Test-first is easier in some languages than others, as unit tests are easier in this languages.

I hope this helps!

[โ€“] [email protected] 2 points 1 month ago* (last edited 1 month ago)

100% this.

The majority of the times if you are struggling with unit tests it's because your code isn't testable.

It felt weird at first writing code with tests in mind but, as you say, it makes your code much nicer to consume in the long run.