Preheat the oven to 375°F (190°C or 180°C for a fan oven).
Cook the macaroni in boiling salted water according to package directions.
Add the broccoli to the pasta for the last three minutes of cooking time.
Drain the broccoli and pasta, return to the saucepan and set aside.
Melt the butter in a medium saucepan over medium heat.
Sauté the onion until it begins to soften.
Sprinkle with the flour and stir through.
Cook for a moment or two.
Gradually add the milk, a bit at at time, stirring and allowing to thicken after each addition.
When the sauce looks creamy and fairly thick but still pourable, add the pesto and 3 ounces of the cheese.
Cook, stirring constantly, until the cheese melts.
If the sauce is too thick, gradually add a bit more milk until the desired consistency is reached.
Pour the sauce over the macaroni and broccoli and gently stir to coat.
Transfer the broccoli macaroni and cheese to a casserole dish.
Cover with sliced tomatoes, if using.
Sprinkle the casserole with the remaining ounce of cheese.
Bake in the oven for ten to fifteen minutes or until the cheese turns a light golden brown.