In a Layer 2 snake test, each port is an access port. That means that the physical connection between port 2 and port 3 doesn’t require that the VLANs ports 2 and 3 be the same.
Think about two switches connected to each other via an access port. There is no requirement that the VLAN on both ends of such a link be the same. Only trunk ports require that the allowed VLANs on both ends be the same. That is why ports 2 and 3 can be on different VLANs, even though they’re connected.
This however would be unusual in a production network topology, as keeping VLAN IDs consistent is important to avoid misconfigurations. But for the snake test, it will work just fine.
If you test at layer 3, then you would create multiple VRFs, one for each pair of ports. VRFs are kind of like VLANs for routers. Each VRF is a different routing domain. If you configure each pair on a different VRF, and you configure routing appropriately, you can route traffic over the same path as in the Layer 2 test.
To find out more about VRFs, take a look at this lesson:
I hope this has been helpful!