They don't need sun on the fruit itself to get sweet, but it can be helpful. What's most important is that the plant itself gets plenty of sun (the fruits will still get sweet if they're shaded by the plant's own leaves, if the plant gets plenty of sun). However, it's likely that they might get even sweeter and more tart with the sun exposure.
Sun on the fruits can cause sunscald for tomatoes and peppers, but it does seem to impact the flavor to a degree (but again, it's mostly the plant's exposure to sun that matters). If the plant is shaded, too (even part of the plant), that can impact flavor significantly on the shaded portion of the plant.
Not all kinds of plants are affected by sun equally the same way. This is even true for different breeds of the same species. The Green Giant tomato, for instance, needs a lot more sun on/near the fruit than some tomatoes do.
Our grapes shaded by leaves still get sweet. The ones with more sun taste similar, but it's possible they might be a bit sweeter (but it's not obvious).
When our thornless blackberries get more sun, they tend to taste a lot better (but they don't taste terribly sweeter; just better).
This answer is just based on personal gardening experience.
Warm soil can help plants (tomatoes and watermelon, anyway) to get sweeter fruit, too (it works better with some varieties and not so well with others, but it seems to work well with most varieties). You can use such as black plastic to help warm the soil. The proper fertilizer/nutrient balance can also help.