Answers for "unit tests in python"

2

how to make a unit converter in python

# Python Program for simple Unit Converter

num1 = input('Enter the value: ')
unit1 = input('Which unit do you want it converted from:  ')
unit2 = input('Which unit do you want it converted to: ')

if unit1 == "cm" and unit2 == "m":
    ans = float(num1)/100
    print(ans)
elif unit1 == "mm" and unit2 == "cm":
    ans = float(num1)/10
    print(ans)
elif unit1 == "m" and unit2 == "cm":
    ans = float(num1)*100
    print(ans)
elif unit1 == "cm" and unit2 == "mm":
    ans = float(num1)*10
    print(ans)
elif unit1 == "mm" and unit2 == "m":
    ans = float(num1)/1000
    print(ans)
elif unit1 == "m" and unit2 == "mm":
    ans = float(num1)*1000
    print(ans)
elif unit1 == "km" and unit2 == "m":
    ans = float(num1)*1000
    print(ans)
elif unit1 == "m" and unit2 == "km":
    ans = float(num1)/1000
    print(ans)
elif unit1 == "mm" and unit2 == "km":
    ans = float(num1)/1000000
    print(ans)
elif unit1 == "ft" and unit2 == "cm":
    ans = float(num1)*30.48
    print(ans)
elif unit1 == "ft" and unit2 == "mm":
    ans = float(num1)*304.8
    print(ans)
elif unit1 == "ft" and unit2 == "inch":
    ans = float(num1)*12
    print(ans)
elif unit1 == "inch" and unit2 == "cm":
    ans = float(num1)*2.54
elif unit1 == "inch" and unit2 == "mm":
    ans = float(num1)*25.4
Posted by: Guest on May-29-2020
1

unittest python how set test failed

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
Posted by: Guest on March-06-2020
0

unit testing python

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
Posted by: Guest on October-28-2021
1

python unittest setUpClass

class MyUnitTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        do_something_expensive_for_all_sets_of_tests()

class MyFirstSetOfTests(MyUnitTest):
    @classmethod
    def setUpClass(cls):
        super(MyFirstSetOfTests, cls).setUpClass()
        do_something_expensive_for_just_these_first_tests()
Posted by: Guest on April-28-2020
1

self.assertequal python

'''
In other words, assertEquals is a function to check if two variables are equal, for purposes of automated testing:
'''

def assertEquals(var1, var2):
    if var1 == var2:
        return True
    else:
        return False
Posted by: Guest on August-05-2020
-1

python testing

>>> assert sum([1, 2, 3]) == 6, "Should be 6"
Posted by: Guest on March-02-2021

Python Answers by Framework

Browse Popular Code Answers by Language