Unit test Flashcards

1
Q

unit test

A

Work with existing or new tests written with the unit test module.

Understand the differences between testing types and when to apply them.

Describe some of the common challenges when testing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

unit test script

A

import unittest

class TestAssertions(unittest.TestCase):

def test_equals(self):
    self.assertEqual("one string", "one string")

if __name__ == ‘__main__’:
unittest.main()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Run tests

A

There are two ways to run a test file. Let’s look again at the end of the test_assertions.py file, where the unittest.main() call allows running the tests by executing the file with Python:

1-The test run is possible because of the if block at the end, where the condition is only met when running the script directly with Python. Let’s check the output when running it in that way:

2-Another way to run tests with the unittest module is by using it with the Python executable. This time, it isn’t necessary to specify the filename because tests can be discovered automatically:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

naming conventions

Test discovery is only possible when you follow naming conventions for methods, classes, and files. If the file does not start with the word test, it won’t get executed.

A

naming conventions

The convention is that they need to be prefixed with test. Although it isn’t required, test classes use camel-casing, and test methods are lower-case, and words are separated with an underscore. For example, here’s how a test for customer accounts that verify creation and deletion could look:

class TestAccounts(unittest.TestCase):

def test_creation(self):
    self.assertTrue(account.create())

def test_deletion(self):
    self.assertTrue(account.delete())
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Assertions and assert methods

note:Python data structures like dictionaries and lists evaluate as true when they have at least one item in them and false when they are empty. Avoid evaluating data structures in this way with assertTrue() and assertFalse() to prevent unexpected items from going undetected. It is preferable to be as accurate as possible when testing.

A

t’s essential to use assert methods instead of Python’s built-in assert() function to have rich reporting when failures happen. The test_assertion.py example uses self.assertEqual(), one of the many special methods from the unittest.TestCase class to ensure that two values are equal:

self.assertTrue(value): Ensure that value is true.
self.assertFalse(value): Ensure that value is false.
self.assertNotEqual(a, b): Check that a and b aren’t equal.

*** assert true method tests if the calculation is true or false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

unit test

A

Integration testing

Functional testing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Integration testing

A

Here are some accepted norms about integration testing:

Not as fast as unit tests, and they tend to require more setup upfront before running.
Functions, methods, or classes are tested with functionality in other functions, methods, or classes.
An external service might be used, but not all the services for the application.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

functional testing

A

Not as fast as unit tests, and they tend to require more setup upfront before running.
Functions, methods, or classes are tested with functionality in other functions, methods, or classes.
An external service might be used, but not all the services for the application.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is one common side-effect for functions when there are no tests involved?

A

Functions can grow in complexity and size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a recognizable aspect in functions and methods when testing is involved?

A

hey tend to have a single responsibility instead of doing many different things

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the correct Python invocation to use test auto discovery?

A

python -m unittest

How well did you know this?
1
Not at all
2
3
4
5
Perfectly