4343 measurements, which some output formats require.
4444"""
4545
46+ import os .path
47+ import random
48+
4649# Import openhtf with an abbreviated name, as we'll be using a bunch of stuff
4750# from it throughout our test scripts. See __all__ at the top of
4851# openhtf/__init__.py for details on what's in top-of-module namespace.
49- import random
50-
5152import openhtf as htf
52-
5353# Import this output mechanism as it's the specific one we want to use.
5454from openhtf .output .callbacks import json_factory
55-
5655# You won't normally need to import this, see validators.py example for
5756# more details. It's used for the inline measurement declaration example
5857# below, but normally you'll only import it when you want to define custom
@@ -94,9 +93,11 @@ def lots_of_measurements(test):
9493# describing the measurement. Validators can get quite complex, for more
9594# details, see the validators.py example.
9695@htf .measures (
97- htf .Measurement ('validated_measurement' ).in_range (
98- 0 ,
99- 10 ).doc ('This measurement is validated.' ).with_units (htf .units .SECOND ))
96+ htf .Measurement ('validated_measurement' )
97+ .in_range (0 , 10 )
98+ .doc ('This measurement is validated.' )
99+ .with_units (htf .units .SECOND )
100+ )
100101def measure_seconds (test ):
101102 # The 'outcome' of this measurement in the test_record result will be a PASS
102103 # because its value passes the validator specified (0 <= 5 <= 10).
@@ -112,7 +113,8 @@ def measure_seconds(test):
112113 'inline_kwargs' ,
113114 docstring = 'This measurement is declared inline!' ,
114115 units = htf .units .HERTZ ,
115- validators = [validators .in_range (0 , 10 )])
116+ validators = [validators .in_range (0 , 10 )],
117+ )
116118@htf .measures ('another_inline' , docstring = 'Because why not?' )
117119def inline_phase (test ):
118120 """Phase that declares a measurements validators as a keyword argument."""
@@ -128,7 +130,8 @@ def inline_phase(test):
128130# A multidim measurement including how to convert to a pandas dataframe and
129131# a numpy array.
130132@htf .measures (
131- htf .Measurement ('power_time_series' ).with_dimensions ('ms' , 'V' , 'A' ))
133+ htf .Measurement ('power_time_series' ).with_dimensions ('ms' , 'V' , 'A' )
134+ )
132135@htf .measures (htf .Measurement ('average_voltage' ).with_units ('V' ))
133136@htf .measures (htf .Measurement ('average_current' ).with_units ('A' ))
134137@htf .measures (htf .Measurement ('resistance' ).with_units ('ohm' ).in_range (9 , 11 ))
@@ -138,7 +141,7 @@ def multdim_measurements(test):
138141 for t in range (10 ):
139142 resistance = 10
140143 voltage = 10 + 10.0 * t
141- current = voltage / resistance + .01 * random .random ()
144+ current = voltage / resistance + 0 .01 * random .random ()
142145 dimensions = (t , voltage , current )
143146 test .measurements ['power_time_series' ][dimensions ] = 0
144147
@@ -158,39 +161,51 @@ def multdim_measurements(test):
158161
159162 # Finally, let's estimate the resistance
160163 test .measurements ['resistance' ] = (
161- test .measurements ['average_voltage' ] /
162- test .measurements ['average_current' ])
164+ test .measurements ['average_voltage' ]
165+ / test .measurements ['average_current' ]
166+ )
163167
164168
165169# Marginal measurements can be used to obtain a finer granularity of by how much
166170# a measurement is passing. Marginal measurements have stricter minimum and
167171# maximum limits, which are used to flag measurements/phase/test records as
168172# marginal without affecting the overall phase outcome.
169173@htf .measures (
170- htf .Measurement ('resistance' ).with_units ('ohm' ).in_range (
171- minimum = 5 , maximum = 17 , marginal_minimum = 9 , marginal_maximum = 11 ))
174+ htf .Measurement ('resistance' )
175+ .with_units ('ohm' )
176+ .in_range (minimum = 5 , maximum = 17 , marginal_minimum = 9 , marginal_maximum = 11 )
177+ )
172178def marginal_measurements (test ):
173179 """Phase with a marginal measurement."""
174180 test .measurements .resistance = 13
175181
176182
177- def main ( ):
183+ def create_and_run_test ( output_dir : str = '.' ):
178184 # We instantiate our OpenHTF test with the phases we want to run as args.
179- test = htf .Test (hello_phase , again_phase , lots_of_measurements ,
180- measure_seconds , inline_phase , multdim_measurements )
185+ test = htf .Test (
186+ hello_phase ,
187+ again_phase ,
188+ lots_of_measurements ,
189+ measure_seconds ,
190+ inline_phase ,
191+ multdim_measurements ,
192+ )
181193
182194 # In order to view the result of the test, we have to output it somewhere,
183195 # and a local JSON file is a convenient way to do this. Custom output
184196 # mechanisms can be implemented, but for now we'll just keep it simple.
185197 # This will always output to the same ./measurements.json file, formatted
186198 # slightly for human readability.
187199 test .add_output_callbacks (
188- json_factory .OutputToJSON ('./measurements.json' , indent = 2 ))
200+ json_factory .OutputToJSON (
201+ os .path .join (output_dir , 'measurements.json' ), indent = 2
202+ )
203+ )
189204
190205 # Unlike hello_world.py, where we prompt for a DUT ID, here we'll just
191206 # use an arbitrary one.
192207 test .execute (test_start = lambda : 'MyDutId' )
193208
194209
195210if __name__ == '__main__' :
196- main ()
211+ create_and_run_test ()
0 commit comments