How to use generate_test method in autotest

Best Python code snippet using autotest_python

autograder.py

Source: autograder.py Github

copy

Full Screen

1#!/​usr/​bin/​python32import random3import os4import datetime5import subprocess6import struct7import sys8# https:/​/​stackoverflow.com/​questions/​16444726/​binary-representation-of-float-in-python-bits-not-hex9def binary(num):10 return ''.join('{:0>8b}'.format(c) for c in struct.pack('!d', num))11def generate_test ( filenum, double=1.0, path="./​" ):12 with open("{}tests/​test{}.txt".format(path,filenum), "w") as infile:13 infile.write( str(double) )14 with open("{}answers/​answer{}.txt".format(path,filenum), "w") as outfile:15 bits = binary(double)16 outfile.write( bits[0] + '_' + bits[1:12] + '_' + bits[12:] )17def generate_test_suite():18 os.makedirs("tests", exist_ok=True)19 os.makedirs("answers", exist_ok=True)20 generate_test ( 0, 0.0 )21 generate_test ( 1, 1.0 )22 generate_test ( 2, -2.0 )23 generate_test ( 3, -0.5 )24 generate_test ( 4, -5./​32 )25 generate_test ( 5, 3.625*16 )26def test_doubleToBin ( filenum, path="./​", verbose=False ):27 try:28 with open("{}answers/​answer{}.txt".format(path,filenum), "r") as outfile:29 answer = outfile.read()30 except EnvironmentError: # parent of IOError, OSError31 print ("answers/​answer{}.txt missing".format(filenum))32 try:33 result = subprocess.run(34 ['./​doubleToBin', "tests/​test{}.txt".format(filenum)],35 cwd=path,36 check=True,37 stdout=subprocess.PIPE,38 stderr=subprocess.STDOUT,39 encoding='ASCII',40 timeout=datetime.timedelta(seconds=4).total_seconds(),41 )42 if verbose:43 print (' '.join(result.args))44 print ("answer")45 print (answer)46 print ("result")47 print (result.stdout)48 assert answer.replace('_','') == result.stdout.replace('_',''), "The printed result doesn't match answers/​answer{}.txt. You can add underscores as needed for readability".format(filenum)49 return True50 except subprocess.CalledProcessError as e:51 print (e.stdout)52 print ("Calling ./​doubleToBin returned non-zero exit status.")53 except ValueError as e:54 print (result.stdout)55 print ("Please check your output formatting; it should be formatted as a binary number.")56 except AssertionError as e:57 print (result.stdout)58 print (e.args[0])59 return False60def grade_doubleToBin( path='./​', verbose=False ):61 score = 062 try:63 subprocess.run( ['make', '-B'], cwd=path, check=True, )64 except subprocess.CalledProcessError as e:65 print ("Couldn't compile doubleToBin.")66 return score67 if test_doubleToBin(0,path,verbose):68 score += 269 if test_doubleToBin(1,path,verbose):70 score += 271 if test_doubleToBin(2,path,verbose):72 score += 273 if test_doubleToBin(3,path,verbose):74 score += 275 if test_doubleToBin(4,path,verbose):76 score += 377 if test_doubleToBin(5,path,verbose):78 score += 379 # standard range test80 allpass = True81 for filenum in range(6,10):82 generate_test (83 filenum,84 double = random.uniform(85 -65536.0,86 +65536.0,87 ),88 path=path89 )90 allpass &= test_doubleToBin(filenum,path,verbose)91 if allpass:92 score += 393 # high magnitude test94 allpass = True95 for filenum in range(10,14):96 generate_test (97 filenum,98 double = random.uniform(99 0.,100 sys.float_info.max101 ),102 path=path103 )104 allpass &= test_doubleToBin(filenum,path,verbose)105 if allpass:106 score += 3107 # denormalized range test108 allpass = True109 for filenum in range(14,18):110 generate_test (111 filenum,112 double = random.uniform(113 -sys.float_info.min,114 sys.float_info.min,115 ),116 path=path117 )118 allpass &= test_doubleToBin(filenum,path,verbose)119 if allpass:120 score += 3121 # negative zero test122 generate_test ( 18, -0.0, path=path )123 if test_doubleToBin(16,path,verbose):124 score += 1125 print ("Score on doubleToBin: {} out of 24.".format(score))126 return score127if __name__ == '__main__':128 # generate_test_suite()129 grade_doubleToBin(verbose=True)...

Full Screen

Full Screen

Blogs

Check out the latest blogs from LambdaTest on this topic:

Why Agile Teams Have to Understand How to Analyze and Make adjustments

How do we acquire knowledge? This is one of the seemingly basic but critical questions you and your team members must ask and consider. We are experts; therefore, we understand why we study and what we should learn. However, many of us do not give enough thought to how we learn.

Top 17 Resources To Learn Test Automation

Lack of training is something that creates a major roadblock for a tester. Often, testers working in an organization are all of a sudden forced to learn a new framework or an automation tool whenever a new project demands it. You may be overwhelmed on how to learn test automation, where to start from and how to master test automation for web applications, and mobile applications on a new technology so soon.

Joomla Testing Guide: How To Test Joomla Websites

Before we discuss the Joomla testing, let us understand the fundamentals of Joomla and how this content management system allows you to create and maintain web-based applications or websites without having to write and implement complex coding requirements.

30 Top Automation Testing Tools In 2022

The sky’s the limit (and even beyond that) when you want to run test automation. Technology has developed so much that you can reduce time and stay more productive than you used to 10 years ago. You needn’t put up with the limitations brought to you by Selenium if that’s your go-to automation testing tool. Instead, you can pick from various test automation frameworks and tools to write effective test cases and run them successfully.

Nov’22 Updates: Live With Automation Testing On OTT Streaming Devices, Test On Samsung Galaxy Z Fold4, Galaxy Z Flip4, & More

Hola Testers! Hope you all had a great Thanksgiving weekend! To make this time more memorable, we at LambdaTest have something to offer you as a token of appreciation.

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful