I am using selenium for end to end testing and I can"t get how to use setup_class
and teardown_class
methods.
I need to set up browser in setup_class
method, then perform a bunch of tests defined as class methods and finally quit browser in teardown_class
method.
But logically it seems like a bad solution, because in fact my tests will not work with class, but with object. I pass self
param inside every test method, so I can access objects" vars:
class TestClass:
def setup_class(cls):
pass
def test_buttons(self, data):
# self.$attribute can be used, but not cls.$attribute?
pass
def test_buttons2(self, data):
# self.$attribute can be used, but not cls.$attribute?
pass
def teardown_class(cls):
pass
And it even seems not to be correct to create browser instance for class.. It should be created for every object separately, right?
So, I need to use __init__
and __del__
methods instead of setup_class
and teardown_class
?
How do I correctly setup and teardown for my pytest class with tests? __del__: Questions
How can I make a time delay in Python?
5 answers
I would like to know how to put a time delay in a Python script.
Answer #1
import time
time.sleep(5) # Delays for 5 seconds. You can also use a float value.
Here is another example where something is run approximately once a minute:
import time
while True:
print("This prints once a minute.")
time.sleep(60) # Delay for 1 minute (60 seconds).
Answer #2
You can use the sleep()
function in the time
module. It can take a float argument for sub-second resolution.
from time import sleep
sleep(0.1) # Time in seconds
How to delete a file or folder in Python?
5 answers
How do I delete a file or folder in Python?
Answer #1
os.remove()
removes a file.os.rmdir()
removes an empty directory.shutil.rmtree()
deletes a directory and all its contents.
Path
objects from the Python 3.4+ pathlib
module also expose these instance methods:
pathlib.Path.unlink()
removes a file or symbolic link.pathlib.Path.rmdir()
removes an empty directory.
How do I correctly setup and teardown for my pytest class with tests? log: Questions
Python"s equivalent of && (logical-and) in an if-statement
5 answers
Here"s my code:
def front_back(a, b):
# +++your code here+++
if len(a) % 2 == 0 && len(b) % 2 == 0:
return a[:(len(a)/2)] + b[:(len(b)/2)] + a[(len(a)/2):] + b[(len(b)/2):]
else:
#todo! Not yet done. :P
return
I"m getting an error in the IF conditional.
What am I doing wrong?
Answer #1
You would want and
instead of &&
.
Answer #2
Python uses and
and or
conditionals.
i.e.
if foo == "abc" and bar == "bac" or zoo == "123":
# do something
How do you get the logical xor of two variables in Python?
5 answers
How do you get the logical xor of two variables in Python?
For example, I have two variables that I expect to be strings. I want to test that only one of them contains a True value (is not None or the empty string):
str1 = raw_input("Enter string one:")
str2 = raw_input("Enter string two:")
if logical_xor(str1, str2):
print "ok"
else:
print "bad"
The ^
operator seems to be bitwise, and not defined on all objects:
>>> 1 ^ 1
0
>>> 2 ^ 1
3
>>> "abc" ^ ""
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for ^: "str" and "str"
Answer #1
If you"re already normalizing the inputs to booleans, then != is xor.
bool(a) != bool(b)