During construction or before a test was ment to happen?
Short answer: no. There have been accidents involving nuclear weapons — more than you might think, actually — but none that resulted in a nuclear detonation.
(Long answer follows. NB: everything I'm about to say relates mostly to the US nuclear programme.)
The US military famously uses the codeword 'Broken Arrow' to refer to accidents involving nuclear weapons (or rather, a specific type of accident — one that doesn't create an imminent risk of nuclear war.) There have been 32 officially-acknowledged BA incidents since 1950, all of them involving either US military or Soviet weapons or nuclear plant. So, the losses of the Soviet submarines K-19 and Kursk, for example, are classified as Broken Arrows: the former because the sub was carrying ballistic missiles; the latter because the Kursk was nuclear-powered.
In October 1958, a RAND Corporation analyst named Fred Iklé published a watershed report, 'On the Risk of an Accidental or Unauthorized Nuclear Detonation', which examined the US nuclear program's safety record up to that point and argued for the implementation of safeguards (both technical and process-based) to ensure that in the event of an accident, US nuclear weapons would 'fail safe' — and to counter the possibility of deliberate but unauthorised detonation. These safeguards include everything from the mechanisms used to arm a nuclear weapon, to the complex, overlapping security systems used to keep these weapons secure, to the command-and-control systems that govern the use of nuclear weapons.
The long story short version is that most nuclear weapons built from the 1960s onwards were designed with physical safeguards (of varying degrees of sophistication) that would prevent a nuclear detonation in the event of an aircraft crash or accidental release. In any case, it's technically very difficult to induce a nuclear detonation — which is why national security experts tend to worry much more about 'dirty bombs' than terrorists detonating a full-fledged nuclear device.
In most cases, if you were to drop a nuclear bomb from an aircraft without arming it first, the high explosive component would detonate on impact but it wouldn't result in a nuclear event. That kind of explosion would, however, likely result in fissile material being blasted over a wide area — and so far, that kind of radiocative contamination been the worst result of any nuclear weapons accident. In 1966, for example, a USAF B-52 bomber carrying four hydrogen bombs collided with a KC-135 tanker over Palomares, Spain; three of the bombs fell on land, and one into the Mediterranean. One of the bombs fell into the Mediterranean and was recovered several months later; the other three were found near Palomares. Of those, two detonated (explosively but non-nuclear) on impact, causing radioactive contamination of the local area.
However: there's a pretty good argument that, at many points over the years, these safeguards have been woefully deficient — and that blind luck plays a significant role in the absence of a catastrophic nuclear accident.
A couple of years ago, investigative reporter Eric Schlosser published a truly fascinating book called Command and Control: The Damascus Incident and the Illusion of Safety. The book does several things extremely well: it's a concise history of the US nuclear weapons programme, and the politicking and inter-service rivalries involved in it; it's a methodical investigation into the development and deficiencies of US nuclear safeguards; and it's a detailed account of one specific nuclear incident — a 1980 fire in a missile silo near Damascus, Arkansas. I heartily recommend reading it — though be warned, it will do precious little to help you sleep easy at night...