bc it seems no one is telling y'all some of this:
You do need to make a gauge swatch, for anything that's meant to fit a human body especially, but also to check whether your yarn + needles combo is going to make the kind of fabric you want. If they don't, you need to do it again with another needle size, and check that, regardless of whether the pattern says you're already using the "right" size. Yes, even if you're using exactly the same yarn the designer did.
The swatch needs to include whatever major techniques are in the project. If it's got big sections of stockinette and of garter, you need to swatch both. If parts of the project are knit in the round and parts worked flat, you need to swatch both. Many people's knit & purl gauges are very different!
If it's heavily cabled, you need to do a swatch of the cable pattern, several repeats both wide and tall. Lace? Swatch a big chunk of that lace pattern. Stranded colorwork? Dear gods yes, swatch the actual designs involved, as well as plain stockinette. It's not going to be the same.
Your swatch should be significantly bigger than the area you need to measure, in each technique. So if the pattern says the gauge is 15 stitches and 20 rows for 4", you need to cast on at least 22-24 stitches and work 30+ rows. More than that, if you're not sure your yarn is the same size as the one the designer used.
Using the actual cast-on from the project is a good idea, because it lets you practice it and also see how it interacts with the fabric growing out of it, but it's not going to be part of the gauge measurement. Same goes for any edge treatment/selvage stitches and your bindoff. You'll be measuring a 4" square in the middle of the work, so your math isn't thrown off by distorted stitches near any edges.
Oh, and you need to block your swatches. Ideally in whatever way you'll wash the finished object (good way to check if that yarn really is superwash!) but at least get it soaking wet, squeeze it out, and let dry flat. Don't stretch or hang it unless you'll do that every single time you get the garment damp (lace swatches obvs can/should be stretched for blocking), just let it dry. Now measure it, carefully. Measure in a couple places, and average your results.
I could write a bunch of caveats & exceptions, and no, I don't actually do all of this every time I sit down to knit a sock. I just start the toe & measure once I get going, & decide how many stitches I'll need then. But A) I've made 20+ pairs of socks in the past few years and B) if I'm super wrong & have to unravel 6" of sock, so what? But you do not want to finally get through a complicated sweater yoke, reach the underarms, and try it on, only to find you were way off. Or wash a finished garment & find it grows 3 sizes, or the fabric opens up so much you can see right through it and it hangs wrong. Heaven forbid you steek something before you realize it's the wrong size.
Also, experience. The more you have, the more corners you can cut - but never all of them! And if you're fairly new to the craft, or just to a certain technique, you don't yet know which corners are essential.
I know proper swatching can feel like a waste of time, and yarn. But the yarn is usually recoverable, and the time is worth it.