Chapter 9

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

assertEquals(expected, actual)

Assertions in Python are methods in the 'unittest.TestCase' class. 'assertEqual' takes two arguments and checks whether they are equal.

A superclass Cat defines a method named make_noise(). Its subclass Tiger also defines a method named make_noise(). What will the code below do? tigger = Tiger() tigger.make_noise()

Call the 'make_noise()' method defined in 'Tiger'. When a superclass and subclass have the same method or attribute name, the subclass's will be used unless the 'super()' function is called.

What is the easiest way to run all the test methods in a test class?

Call the unittest.main() function with the class's module name as the argument. In addition to providing the "TestCase' class, the 'unittest' library contains a 'main' function that automatically runs all the test methods within TestCase classes in a module

A class contains an attribute that must be accessed by outside code, but should never be changed by outside code. What is the best solution.

Change the attribute to a getter method to make it a read-only property. A read-only property can be accessed by outside code as if it were an attribute, but cannot be modified.

A game board contains many spaces, and some of those bonus squares that do special things such as drawing cards. Which is most fitting to model this relationship?

Composition and Inheritance. Composition and inheritance are often combined. Here, the game board is a composite that has squares as components, and the bonus square is a more specific subclass of the square superclass.

A photo album is an object that contains one or more photos. Which is most fitting to model this relationship?

Composition. A composition relationship is when an instance of a class is contained within an instance of another class. Here, a phot album is a comoposite that contains photo components.

What will the following line of code do when it runs. unittest.main("library_tests")

Find the module named 'library_tests', and run any test methods in any test classes within it. In addition to providing the 'TestCase' class, the 'unittest' library contains a 'main' function that automatically runs all the test methods within TestCase classes in a module.

A multiple choice question and true-or-false question are both types of quiz question with different data and behavior. Which is more fitting to model this relationship?

Inheritance. An inheritance relationship is when a class receives the attributes and methods of another class as a starting point. Here, a multiple choice question and true-or-false question are both subclasses of the quiz question superclass.

What is special about abstract classes that inherit from the abc.ABC superclass?

They can mark methods as abstract. To declare an abstract method that is required to be overridden by any subclass, a class must inherit from the 'abstract base 'class' 'abc.ABC'

Fix the class declaration so the following program will run.

class StringTests(unittest.TestCase): In order to create unit tests, we to inherit from the TestCase superclass, which is part of the unittest module

In the following test method, highlight the expected value. Be sure to highlight only the value. def test_math_sqrt(self): self.assertEqual(2, math.sqrt(4))

the number 2 would be highlighted. The expected value is what the test is expecting some function or calculation to create. It typically comes first in the 'assertEqual' method.

Which situation is best modeled by an inheritance relationship?

A bee is more specific type of insect with different behaviors. An inheritance relationship is when a clss receives the attributes and methods of another class as a starting point. Here, a bee receives the attributes and methods of insects and adds its own.

What does the @abstractmethod decorator mean when placed before a method?

The method cannot be called directly, and must be overridden by any subclass. An abstract class can only be used as a superclass, and only if all its abstract methods are overridden by its subclass.

Write the declaration line for a subclass named Apple of superclass Fruit

class Apple(Fruit): In Python, a subclass is created by specifying the superclass in parentheses in its declaration.

Which of the following assert methods would be best to check if concatenating the strings "hello" and "world" resulted in "helloworld"?

assertEquals. 'assertEquals' can be used to check if come created value matches what we are expecting. In this case, we would want to check if concatenating "hello" and "world" created the expected value of "helloworld".

magic methods

A magic method's name begins and ends with two underscores. The '__add__' method defines how an instance will work when used to the left of the '+' operator.

Which situation is best modeled by a composition relationship?

A mall has many stores, each of which has its own data and behaviors. composition relationship is when an instance of a class is contained within an instance of another class. Here, a mall is a composite that contains sore components.

A class contains an attribute that can be changed by outside code, but it needs an update() method to run whenever it is changed. What is the best solution?

Add both getter and sette methods to make the attribute into a property. By using a setter method, a property can run code automatically whenever it is modified.

Which of the following is true?

All. A subclass can access attributes of its superclass. A subclass can override an attribute to give it a different value. A subclass can define its own attributes that are not present in the superclass. A subclass can access a superclass's attributes, override their values, and add new values

When looking at aa class, how can you tell whether a method is abstract?

An abstract method has the '@abstractmethod' decorator on the line above it. Any abstract method that must be overridden by subclasses will be marked with the decorator '@abstractmethod'

When running a test module, what does it mean when a test method outputs "FAIL" and lists an "AssertionError"?

An assertion within the test method did not evaluate to True. When a test module is run using 'unttest.main()' every test method inside it will run. If an assertion within a test method evaluates to False, the test will print 'Fail' with an error about the assertion.

attribute

An attribute is a piece of data within a class, usually defined in the '__init__' method by using the 'self' keyword.

How can you tell if a class is a composite by looking at its code?

An instance of another class is stored as an attribute inn its '__init__' method. A composite is a class that contains one or more other class instances (in a composition relationship). Any class with another instance as an attribute is a composite

Which of the following would not be an appropriate test for basic messaging program?

None of the above. All of these tests cover basic functionality that a messaging grogram probably has. Without seeing the program itself, it's hard to tell if this is all the tests we would need, or if there are additional edge cases to cover.

When is a class's __init__ method called?

Whenever an instance of the class is created. The __init___ method is called by an instance when it is first created, to set up any attributes and other data.

How many component instances can be in an instance of a composite class?

Any number of instances of any number of classes. A composite class is any class that uses instances of other classes as attributes, whether just one or many

Which magic method in the User class will be called as part of running this code? new_user = User("username123", 0, 0) print(new_user)

'__str__' the '__str__' magic method defines how to convert an instance to a string. Since 'print's' arguments must be a string, the User class must have '__str__' implemented for its instaance to be used as an argument for print.

Which is true about inheritance and composition?

Both superclasses or subclasses can use composition. Many programs use both composition and inheritance. Either a superclass can store an instance of another class as an attribute, becoming a composite. Either a superclass or subclass can be used as an attribute of another class, becoming a component.

How can a component class within a composite class be used by outside code?

By using the dot operator to access the component attribute as an attribute. By calling a method that redirects functionality to the correct component. By calling a method that used multiple components together to perform an action. Any cla instance within another class is called a component. Components can be used by the composite class in a number of ways. including any of these strategies.

What must any subclass of an abstract class do in order to avoid a runtime error?

It must override any methods marked abstract. For an abstract class to be used as a superclass, its subclass must override its abstract methods. Otherwise, creating an instance of the subclass will causse a runtime error.

magic method equal

The '__eq__' magic method defines how to compare an instance to another value when using the '==' operator. On the third line, the operator is evaluated to compare the two Boxes, resulting in a value of 'True" or 'False' that is then printed.

In composition, what best described the relationship between a composite and its component?

The composite has one or more instances of the component as attributes. A composite is a class that contains one or more other class instances (in a composition relationship).

Which of the following is necessary for the @abstractmethod decorator to be used?

The method's class must be a subclass of 'abc.ABC'. An abstract method must be within an abstract class, which must inherit from the 'abstract base class' 'abc.ABC'

When a unit test method outputs "FAIL" and lists an "AssertionError", what sort of error should you look to fix.

There is likely a logic error in the function being tested. if an assertion within a test method evaluates to false, the test will print 'Fail' with an error about the assertion. That generally means a function being tested gave an unexpected result, meaning a logic error is present.

Which line of code declares a class named Birdhouse>

class Birdhouse: A class is defined by writing the word 'class' followed by its name.

Complete the following test method by writing an assert that test whether the actual and expected values are equal.

self.assertEqual(actual, expected) Assertions in Python are methods in the 'unittest.TestCase' class. to check that an expected and actual value are equal, 'assertEqual' is preferred because it gives a more useful message on failure, but 'assertTrue' can also be used.

Which of the following would be a appropriate name for a test that verified that adding 2 and 3 resulted in 5?

test_add_numbers. Test method names should always start with 'test and describe what feature is being tested..

What happens whenever an instance of a class is created?

the instance's __init__() method runs automatically. the __init__ method is called by an instance when it is first created, to set up any attributed and other data.

properly call a method

to access a method or attribute within a class, the 'self' keyword must be used.


Set pelajaran terkait

Northwestern Europe Ch. 11 Geography

View Set

Principles of Real Estate Chapter 14

View Set

Exam 2 UNH Managerial Accounting

View Set

Lehne Chapter 53: Management of ST-Elevation Myocardial Infarction

View Set

Chapter 26- Drugs for Hypertension

View Set

Random Health Insurance Questions

View Set