A major terrorism trial is set to be held entirely in secret for the first time in British legal history in an unprecedented departure from the principles of open justice, the court of appeal has heard.

The identities of the two defendants charged with serious terror offences are being withheld from the public, and the media are banned from being present in court to report the forthcoming trial against the two men, known only as AB and CD.

The unprecedented secrecy has been imposed on the proceedings after the Crown Prosecution Service obtained legal orders to withhold the names of the defendants and allow the trial to take place in private on the grounds, they said, of national security.

At the court of appeal in London, Anthony Hudson, representing the Guardian and several other media organisations, challenged the orders, which will allow a secret criminal trial to take place for the first time in legal history.

He was appealing under section 159 of the Criminal Justice Act 1988 against orders made by Mr Justice Nicol.

"This appeal raises important issues relating to the constitutional principle of open justice but also the equally important principle of fairness and natural justice," Hudson told the court. "This case is a test of the court's commitment to that constitutional principle.

"The crown has sought and obtained unprecedented orders that the trial of two defendants charged with serious terrorism offences should take place entirely in private with the identities of both defendants withheld and a permanent prohibition on reporting what takes place during the trial.

"No order has ever been made which requires an entire criminal trial to be in private with the media excluded and the defendants unnamed.

"We submit that the orders made involve such a significant departure from the principle of open justice they are inconsistent with the rule of law and democratic accountability."

He said that national security could not be pursued without regard to the values of the society it was seeking to protect.

The crown presented evidence from Mari Reid, unit head of the counter-terrorism team in the special crime and counter-terrorism division of the CPS, as part of its case. She said that if the trial had to be held in public there was a "serious possibility that the trial may not be able to go ahead".

But Hudson told the court: "That is not sufficient; it is not a good enough basis on which a court can be satisfied that there is no other option but to hold the entire trial in private."

Richard Whittam, QC, for the crown said the case involved "clearly exceptional circumstances" which had led to the "exceptional procedures" that had been approved by Nicol.

"It is quite clear that there is jurisdiction for the defendants to be anonymous and there's jurisdiction for a court to sit in private. Whether or not it is appropriate to do so is realistically evidence-dependant.

The court heard that AB and CD were arrested in a high-profile police operation last year and have been charged with serious terrorism offences.

AB is charged under section 5(1) of the Terrorism Act 2006 with engaging in conduct in preparation of terrorist acts between February 2012 and October 2013.

AB is also charged, with CD, of being in possession of documents or records containing information of the kind likely to be used by a person committing or preparing an act of terrorism. This relates to their alleged possession of a document entitled "Bombmaking".

CD is also charged with possession of an improperly obtained UK passport under the Identity Documents Act 2010.

The crown alleges that they may be deterred from prosecuting AB and or CD if the trial is held in public and the identities of the defendants released, and that a public trial will interfere with the administration of justice.

But the evidence on which the crown relied to argue for the secret trial could not be presented in open court, he said. Whittam presented the evidence in private to the court of appeal judges, Lord Justice Gross, Mr Justice Simon and Mr Justice Burnett.

After the day-long hearing Gross said he would give the court's decision on the appeal by the media in a few days and a full judgment at a later date.