[Test] Use context handler form for 'assertRaises'
This commit is contained in:
parent
be9882a765
commit
baa787f7ac
6 changed files with 52 additions and 57 deletions
|
|
@ -88,22 +88,22 @@ class chemkinConverterTest(utilities.CanteraTest):
|
|||
self.checkKinetics(ref, gas, [300, 800, 1450, 2800], [5e3, 1e5, 2e6])
|
||||
|
||||
def test_missingElement(self):
|
||||
self.assertRaises(ck2cti.InputParseError,
|
||||
lambda: convertMech('../data/h2o2_missingElement.inp',
|
||||
outName='h2o2_missingElement.cti',
|
||||
quiet=True))
|
||||
with self.assertRaises(ck2cti.InputParseError):
|
||||
convertMech('../data/h2o2_missingElement.inp',
|
||||
outName='h2o2_missingElement.cti',
|
||||
quiet=True)
|
||||
|
||||
def test_missingThermo(self):
|
||||
self.assertRaises(ck2cti.InputParseError,
|
||||
lambda: convertMech('../data/h2o2_missingThermo.inp',
|
||||
outName='h2o2_missingThermo.cti',
|
||||
quiet=True))
|
||||
with self.assertRaises(ck2cti.InputParseError):
|
||||
convertMech('../data/h2o2_missingThermo.inp',
|
||||
outName='h2o2_missingThermo.cti',
|
||||
quiet=True)
|
||||
|
||||
def test_duplicate_thermo(self):
|
||||
self.assertRaises(ck2cti.InputParseError,
|
||||
lambda: convertMech('../data/duplicate-thermo.inp',
|
||||
outName='duplicate-thermo.cti',
|
||||
quiet=True))
|
||||
with self.assertRaises(ck2cti.InputParseError):
|
||||
convertMech('../data/duplicate-thermo.inp',
|
||||
outName='duplicate-thermo.cti',
|
||||
quiet=True)
|
||||
|
||||
convertMech('../data/duplicate-thermo.inp',
|
||||
outName='duplicate-thermo.cti',
|
||||
|
|
@ -114,10 +114,10 @@ class chemkinConverterTest(utilities.CanteraTest):
|
|||
self.assertTrue(gas.n_reactions, 2)
|
||||
|
||||
def test_duplicate_species(self):
|
||||
self.assertRaises(ck2cti.InputParseError,
|
||||
lambda: convertMech('../data/duplicate-species.inp',
|
||||
outName='duplicate-species.cti',
|
||||
quiet=True))
|
||||
with self.assertRaises(ck2cti.InputParseError):
|
||||
convertMech('../data/duplicate-species.inp',
|
||||
outName='duplicate-species.cti',
|
||||
quiet=True)
|
||||
|
||||
convertMech('../data/duplicate-species.inp',
|
||||
outName='duplicate-species.cti',
|
||||
|
|
@ -149,10 +149,10 @@ class chemkinConverterTest(utilities.CanteraTest):
|
|||
self.assertEqual(list(nu[:,5]), [1, 0, 0, 1, -1, -1])
|
||||
|
||||
def test_unterminatedSections(self):
|
||||
self.assertRaises(ck2cti.InputParseError,
|
||||
lambda: convertMech('../data/unterminated-sections.inp',
|
||||
outName='unterminated-sections.cti',
|
||||
quiet=True))
|
||||
with self.assertRaises(ck2cti.InputParseError):
|
||||
convertMech('../data/unterminated-sections.inp',
|
||||
outName='unterminated-sections.cti',
|
||||
quiet=True)
|
||||
|
||||
convertMech('../data/unterminated-sections.inp',
|
||||
outName='unterminated-sections.cti',
|
||||
|
|
@ -163,10 +163,10 @@ class chemkinConverterTest(utilities.CanteraTest):
|
|||
self.assertEqual(gas.n_reactions, 2)
|
||||
|
||||
def test_unterminatedSections2(self):
|
||||
self.assertRaises(ck2cti.InputParseError,
|
||||
lambda: convertMech('../data/unterminated-sections2.inp',
|
||||
outName='unterminated-sections2.cti',
|
||||
quiet=True))
|
||||
with self.assertRaises(ck2cti.InputParseError):
|
||||
convertMech('../data/unterminated-sections2.inp',
|
||||
outName='unterminated-sections2.cti',
|
||||
quiet=True)
|
||||
|
||||
convertMech('../data/unterminated-sections2.inp',
|
||||
outName='unterminated-sections2.cti',
|
||||
|
|
@ -284,25 +284,19 @@ class chemkinConverterTest(utilities.CanteraTest):
|
|||
self.assertTrue(d > 0.0)
|
||||
|
||||
def test_transport_missing_species(self):
|
||||
def convert():
|
||||
with self.assertRaises(ck2cti.InputParseError):
|
||||
convertMech('../../data/inputs/h2o2.inp',
|
||||
transportFile='../data/h2o2-missing-species-tran.dat',
|
||||
outName='h2o2_transport_missing_species.cti',
|
||||
quiet=True)
|
||||
|
||||
self.assertRaises(ck2cti.InputParseError, convert)
|
||||
|
||||
def test_transport_duplicate_species(self):
|
||||
def convert():
|
||||
with self.assertRaises(ck2cti.InputParseError):
|
||||
convertMech('../../data/inputs/h2o2.inp',
|
||||
transportFile='../data/h2o2-duplicate-species-tran.dat',
|
||||
outName='h2o2_transport_duplicate_species.cti',
|
||||
quiet=True)
|
||||
|
||||
# This should fail
|
||||
self.assertRaises(ck2cti.InputParseError, convert)
|
||||
|
||||
# This should succeed
|
||||
convertMech('../../data/inputs/h2o2.inp',
|
||||
transportFile='../data/h2o2-duplicate-species-tran.dat',
|
||||
outName='h2o2_transport_duplicate_species.cti',
|
||||
|
|
@ -310,13 +304,12 @@ class chemkinConverterTest(utilities.CanteraTest):
|
|||
permissive=True)
|
||||
|
||||
def test_transport_bad_geometry(self):
|
||||
def convert():
|
||||
with self.assertRaises(ck2cti.InputParseError):
|
||||
convertMech('../../data/inputs/h2o2.inp',
|
||||
transportFile='../data/h2o2-bad-geometry-tran.dat',
|
||||
outName='h2o2_transport_bad_geometry.cti',
|
||||
quiet=True)
|
||||
|
||||
self.assertRaises(ck2cti.InputParseError, convert)
|
||||
|
||||
class CtmlConverterTest(utilities.CanteraTest):
|
||||
def test_sofc(self):
|
||||
|
|
|
|||
|
|
@ -38,7 +38,8 @@ class TestFunc1(utilities.CanteraTest):
|
|||
raise ValueError('bad')
|
||||
|
||||
f = ct.Func1(fails)
|
||||
self.assertRaises(ValueError, f, 0.1)
|
||||
with self.assertRaises(ValueError):
|
||||
f(0.1)
|
||||
|
||||
def test_unpicklable(self):
|
||||
import pickle
|
||||
|
|
|
|||
|
|
@ -51,8 +51,10 @@ class TestKinetics(utilities.CanteraTest):
|
|||
self.assertNear(self.phase.reaction_type(2), 1) # elementary
|
||||
self.assertNear(self.phase.reaction_type(19), 4) # falloff
|
||||
|
||||
self.assertRaises(ValueError, self.phase.reaction_type, 33)
|
||||
self.assertRaises(ValueError, self.phase.reaction_type, -2)
|
||||
with self.assertRaises(ValueError):
|
||||
self.phase.reaction_type(33)
|
||||
with self.assertRaises(ValueError):
|
||||
self.phase.reaction_type(-2)
|
||||
|
||||
def test_reaction_equations(self):
|
||||
self.assertEqual(self.phase.n_reactions,
|
||||
|
|
|
|||
|
|
@ -38,10 +38,10 @@ class TestReactor(utilities.CanteraTest):
|
|||
|
||||
def test_insert(self):
|
||||
R = self.reactorClass()
|
||||
f1 = lambda r: r.T
|
||||
f2 = lambda r: r.kinetics.net_production_rates
|
||||
self.assertRaises(Exception, f1, R)
|
||||
self.assertRaises(Exception, f2, R)
|
||||
with self.assertRaises(Exception):
|
||||
R.T
|
||||
with self.assertRaises(Exception):
|
||||
R.kinetics.net_production_rates
|
||||
|
||||
g = ct.Solution('h2o2.xml')
|
||||
g.TP = 300, 101325
|
||||
|
|
@ -782,14 +782,16 @@ class TestWallKinetics(utilities.CanteraTest):
|
|||
C_left = self.w.left.coverages
|
||||
|
||||
self.assertEqual(self.w.right.kinetics, None)
|
||||
self.assertRaises(Exception, lambda: self.w.right.coverages)
|
||||
with self.assertRaises(Exception):
|
||||
self.w.right.coverages
|
||||
|
||||
self.make_reactors()
|
||||
self.w.right.kinetics = self.interface
|
||||
self.w.right.coverages = C
|
||||
self.assertArrayNear(self.w.right.coverages, C)
|
||||
self.assertEqual(self.w.left.kinetics, None)
|
||||
self.assertRaises(Exception, lambda: self.w.left.coverages)
|
||||
with self.assertRaises(Exception):
|
||||
self.w.left.coverages
|
||||
self.net.advance(1e-5)
|
||||
C_right = self.w.right.coverages
|
||||
|
||||
|
|
|
|||
|
|
@ -35,8 +35,10 @@ class TestThermoPhase(utilities.CanteraTest):
|
|||
kSpec = self.phase.species_index(species)
|
||||
self.assertEqual(self.phase.n_atoms(kSpec, mElem), n)
|
||||
|
||||
self.assertRaises(ValueError, lambda: self.phase.n_atoms('C', 'H2'))
|
||||
self.assertRaises(ValueError, lambda: self.phase.n_atoms('H', 'CH4'))
|
||||
with self.assertRaises(ValueError):
|
||||
self.phase.n_atoms('C', 'H2')
|
||||
with self.assertRaises(ValueError):
|
||||
self.phase.n_atoms('H', 'CH4')
|
||||
|
||||
def test_weights(self):
|
||||
atomic_weights = self.phase.atomic_weights
|
||||
|
|
@ -64,11 +66,9 @@ class TestThermoPhase(utilities.CanteraTest):
|
|||
self.assertNear(X[0], 0.5)
|
||||
self.assertNear(X[3], 0.5)
|
||||
|
||||
def set_bad():
|
||||
with self.assertRaises(Exception):
|
||||
self.phase.X = 'H2:1.0, CO2:1.5'
|
||||
|
||||
self.assertRaises(Exception, set_bad)
|
||||
|
||||
def test_setCompositionStringBad(self):
|
||||
X0 = self.phase.X
|
||||
with self.assertRaises(Exception):
|
||||
|
|
@ -147,14 +147,11 @@ class TestThermoPhase(utilities.CanteraTest):
|
|||
|
||||
def test_badLength(self):
|
||||
X = np.zeros(5)
|
||||
def set_X():
|
||||
with self.assertRaises(ValueError):
|
||||
self.phase.X = X
|
||||
def set_Y():
|
||||
with self.assertRaises(ValueError):
|
||||
self.phase.Y = X
|
||||
|
||||
self.assertRaises(ValueError, set_X)
|
||||
self.assertRaises(ValueError, set_Y)
|
||||
|
||||
def test_mass_basis(self):
|
||||
self.assertEqual(self.phase.basis, 'mass')
|
||||
self.assertEqual(self.phase.density_mass, self.phase.density)
|
||||
|
|
@ -518,5 +515,5 @@ class ImportTest(utilities.CanteraTest):
|
|||
self.check(gas2, 'notair', 900, 5*101325, 7, 2)
|
||||
|
||||
def test_checkReactionBalance(self):
|
||||
self.assertRaises(Exception,
|
||||
lambda: ct.Solution('../data/h2o2_unbalancedReaction.xml'))
|
||||
with self.assertRaises(Exception):
|
||||
ct.Solution('../data/h2o2_unbalancedReaction.xml')
|
||||
|
|
|
|||
|
|
@ -33,8 +33,8 @@ class TestTransport(utilities.CanteraTest):
|
|||
self.assertArrayNear(Dbin1, Dbin1.T)
|
||||
|
||||
def test_multiComponent(self):
|
||||
self.assertRaises(Exception,
|
||||
lambda: self.phase.Multi_diff_coeffs)
|
||||
with self.assertRaises(Exception):
|
||||
self.phase.Multi_diff_coeffs
|
||||
|
||||
self.assertArrayNear(self.phase.thermal_diff_coeffs,
|
||||
np.zeros(self.phase.n_species))
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue